Re:You Can't JavaScript Under Pressure
by downtowncanada on Thursday October 03, 2013 @12:01PM
(#6489862)
if(typeof i[p] === typeof 3){//it's an int
sum += i[p];
}else if(typeof i[p] === typeof [3]){//it's an array recursion(i[p]);
}pretty simple no?
That would've been simpler, but I seemed to get away with typeof(i[p]) === "object" and typeof(i[p]) === 1*typeof(i[p]) to test if it was an int.
I dread to think what obscure code I've left behind.
Re:You Can't JavaScript Under Pressure
by chrisrhoden on Thursday October 03, 2013 @06:22PM
(#6492554)
no, in this case the first will match on all numbers and the second will match on all objects.
You should learn a little more about JS before you call this simple.
n % 1 == 0Tried that first, but that will fail on the "false" case.
Re:You Can't JavaScript Under Pressure
by badman_ting on Thursday October 03, 2013 @02:10PM
(#6490790)
It needs a triple-equals, because false == 0, but false !== 0
Re:You Can't JavaScript Under Pressure
by Justin_Time on Thursday October 03, 2013 @11:02AM
(#6489357)
typeof n === 'number'
It didn't actually require differentiating between integers and floats, just numbers and other types.
This typeof stuff (and having number instead of integer, string, etc) was the hardest part of this for me.
Normally javascript function inputs aren't this messy.
> typeof 5.4
"number"Yea, this is what I realized once I saw the puzzle was throwing junk data like "false" on #5.
I mentally skipped over Justin_Time's solution, figuring I would just hit a float error case because it was going to be a stickler about integers.
Re:You Can't JavaScript Under Pressure
by chrisrhoden on Thursday October 03, 2013 @11:26AM
(#6489550)
n===~~n
Edit: Read the initial requirements of the puzzle, it says sum all the integers, not all the numbers. People using (typeof i[x] == 'number') just got lucky because the test-cases didn't exercise the full requirements of the puzzle ;)