News for hackers. Stuff that matters.
Re:You Can't JavaScript Under Pressure by kirse on Thursday October 03, 2013 @11:00AM (#6489345)
Fun little puzzles. First 4 went pretty quickly (~8:00) and then the last one took ~7 minutes Googling around for this so I could test if a variable was an integer...

  n===+n && n===(n|0)
Wish I could see my answers after the fact though!

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 ;)

  // i will be an array, containing integers and/or arrays like itself.
  // Sum all the integers you find, anywhere in the nest of arrays.
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?

Re:You Can't JavaScript Under Pressure by cs02rm0 on Thursday October 03, 2013 @12:15PM (#6489964)
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.

Re:You Can't JavaScript Under Pressure by kentor on Thursday October 03, 2013 @11:18AM (#6489490)

    n % 1 == 0
Re:You Can't JavaScript Under Pressure by kirse on Thursday October 03, 2013 @11:22AM (#6489515)
Tried 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.

Re:You Can't JavaScript Under Pressure by iainnash on Thursday October 03, 2013 @11:08AM (#6489411)
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.

Re:You Can't JavaScript Under Pressure by solox3 on Thursday October 03, 2013 @11:04AM (#6489375)

    > typeof 5.4
      "number"
Re:You Can't JavaScript Under Pressure by kirse on Thursday October 03, 2013 @11:08AM (#6489418)
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