Rosetta code exponential generator

the function passes all test except the last one, which is exponentialGenerator(25) should return 784
and it returns 729 instead, why?
Your code so far


function exponentialGenerator(n) {
var arr = [];
for (let i = 2; arr.length <= n; i++) {
if (!Number.isInteger(Math.pow(i, 2/3))) {
arr.push(Math.pow(i, 2))}
}
return arr[n]
}
console.log(exponentialGenerator(25))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36.

Challenge: Generator/Exponential

Link to the challenge:

If you add a console.log to see what values are in your array, you are adding some cubes that you shouldn’t be:

function exponentialGenerator(n) {
  var arr = [];
  for (let i = 2; arr.length <= n; i++) {
    if (!Number.isInteger(Math.pow(i, 2/3))) {
        console.log(Math.pow(i,2))
      arr.push(Math.pow(i, 2))
    }
  }
  return arr[n]
}

console.log(exponentialGenerator(14));

It looks like you are getting roundoff in your cube rooting.

When you get a more accurate test for cubes, you’ll need to fix your ‘off by one’ in the loop bounds (<= vs <).

@JeremyLT yeah you’re right

but what does this do then? what’s the problem here that it’s not filtering?
ik you could have an array of cubes and use .filter and .includes but what’s the problem particularly here?

Try console.log(Math.pow(8, 2/3)) and see what pops out

that’s rude, if i use round normally it will also be an integer for non cubic numbers, i always have those damn round off problems :confused:, how can i prevent them (if there’s any way, even at other laguages with int keyword or stuff)

guess ill have to go the cube array way then

You can always check if the value and the value rounded to the nearest integer are really close together.

how can you check if they’re really close? there’s a buit-in method or just iterating?

you will need to make the square root and the third power separately

Two numbers are really close if their difference is really tiny.