3 ms·
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 int
by kirse 13y 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.
- Justin_Time 13y agotypeof n === 'number' It didn't actually require differentiating between integers and floats, just numbers and other types.
- solox3 13y ago> typeof 5.4 "number"
- kirse 13y agoYea, 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.
- iainnash 13y agoThis 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.
- deleted 13y ago[deleted]
- kentor 13y agon % 1 == 0
- kirse 13y agoTried that first, but that will fail on the "false" case.
- badman_ting 13y agoIt needs a triple-equals, because false == 0, but false !== 0
- chrisrhoden 13y agon===~~n
- deleted 13y ago[deleted]
- downtowncanada 13y agoif(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?
- cs02rm0 13y agoThat 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.
- chrisrhoden 13y agono, 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.