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.
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).
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