DEV Community

Discussion on: Solving "Arguments Optional" / freeCodeCamp Algorithm Challenges

Collapse
 
ttatsf profile image
tatsuo fukuchi • Edited

As you use Number.isInteger() , you'll miss float numbers.
You can use typeof / Object.prototype.toString.call() to detect number:

Number.isInteger(0.1)  // false
typeof 0.1 === "number"  // true
Object.prototype.toString.call(0.1) ==="[object Number]"  // true

And , here another solution:

const addTogether = ( a, ...arr ) =>
  typeof a !== "number" ? undefined
  : arr.length === 0 ? addT( a )
  : addT( a )( arr[0] )

const addT = a => b =>
  typeof b !== "number"  ? undefined
  : a + b