Rosetta Code: 100 doors: expected output?

Tell us what’s happening:
I think I have the correct code but not sure what kind of output the challenge wants. It says,

Return the final result in an array, with only the door number included in the array if it is open.

I am assuming I need to return an array only with indexes of open doors, am I wrong?

Your code so far


function getFinalOpenedDoors () {
  let doors = [];
  let finalDoors = [];

  for(let i=0; i<100; i++) {
    doors.push(false);
  }

  for(let i=1; i <= 100; i++) {
    for(let j=i-1; j < 100; j += i) {
        doors[i-1] = !doors[i-1];
    }
  }

  for(let i=0; i<doors.length; i++) {
    if(doors[i])
      finalDoors.push(i);
  }

  return finalDoors;
  // Good luck!
}

getFinalOpenedDoors();

Your browser information:

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

Link to the challenge:

Your understanding is correct, but your code is incorrect.

Thanks for clarifying, my code above somehow got mixed badly with previous attempt :frowning:

Just be aware that the door number is not necessarily the index number.

Hi your code needs two fixes. First one is

doors[i-1] = !doors[i-1];

this should be

doors[j] = !doors[j];

and the other is

finalDoors.push(i);

should be

finalDoors.push(i+1);

since it wants the door number not the zero based index.