Hacker News new | ask | show | jobs
by kirse 4635 days ago
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.
4 comments

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.

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 == 0
Tried that first, but that will fail on the "false" case.
It needs a triple-equals, because false == 0, but false !== 0
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.

n===~~n