Simple JS Help - 100 doors

Hello, can you please help me?
The logic for 100 doors is: every Perfect Square number will be open at the end.
Therefore, I’m trying to write a code that only displays perfect squares (1, 4, 9, 16, …)
1 - Create array doors
2 - if number is perfect square then push into array
3 - return array doors

Can someone please tell me what is wrong with my code?

Thanks!

function getFinalOpenedDoors(numDoors) {

  var doors = [];

  for (numDoors = 0; numDoors < 100; i++) {

    if (Math.sqrt(numDoors) % 1 === 0){

      doors.push[numDoors];

    }

  }

  return doors;

}

Your browser information:

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

Challenge: 100 doors

Link to the challenge:

Then why are you using square root? Just square the rational numbers up to and including the number of doors.

Thanks for the answer Randell!
Can you send an example code on how you would do that, please?
Thanks!

You’d start your loop variable at 1, incrementing by 1 at each loop, pushing the square of the loop variable to your array, and stopping the loop once the loop variable is greater than the square root of the max number of doors (100).

I would stop lopping once the square (not the square root) of the loop variable is greater than the max number of doors.

x*x > max
x > Math.sqrt(max)

I don’t know which is more performant, but mathematically they’re the same, right?

If you do each of those every time, then expect that

x*x

is faster than

Math.sqrt(max)

over a somewhat large ranges (provided that you don’t hit overflow and everything goes to hell). However, if you save the value, then you will get better performance when using

let bound = Math.sqrt(max)
....
x > bound

in general.