3 ms·
I think this looks about as clean as I expected, I definitely like the look of the list comprehension better. (But of course, this is just a superficial syntac
by omaranto 7y ago
I think this looks about as clean as I expected, I definitely like the look of the list comprehension better.
(But of course, this is just a superficial syntactic matter: there is no essential difference.)
Does JavaScript not come with a .flatmap or similar method? I'm not sure I like having to count the nesting to pass the appropriate number to .flat. Also building up a nested list and then flattening is less efficient than the comprehension which builds the flattened version directly.
The +0.5 is to deal with floating point. Sadly the cube root isn't always exact enough to be an integer when it should, so I used floor(cube root + 0.5) to round to the near integer and then checked if that rounded integer really was the cube root.
Have you tried running this? Is Number.isInteger lenient enough to actually find the solution?