Rosetta Code: 100 doors Should Return An Array

Tell us what’s happening:
It keeps telling me: “getFinalOpenedDoors should return an array.”
On my computer it does, but it does not pass the test here.
Please help.

Your code so far


function getFinalOpenedDoors(numDoors) {
let doors = new Array(numDoors);
for (let i = 0; i < numDoors; i++) {
    doors[i] = "Closed";
}

for (let j = 1; j <= numDoors; j++) {
    for (let z = j-1; z < numDoors; z += j) {
        if (doors[z] === "Closed") {
            doors[z] = 'OPEN'; 
        } else {
            doors[z] = "Closed";
        }
    }
} 
var newdoors = [];
var newdoors = new Array();
for (y = 0; y < numDoors; y++) {
    var ddd = doors[y];
    if (doors[y] === 'OPEN') {
        newdoors.push(doors[y]);
    }
}
var ccc = newdoors;
return ccc;   
}

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: 100 doors

Link to the challenge:

Your code is failing the 2nd test, because it does not complete. The reason it does not complete is that you have not defined the variable y in the he for loop.

Your variable y is not declared.

Once you fix this, you can put

console.log(getFinalOpenedDoors(100));

at the bottom of your code and see that the result does not match the requested result. You should be returning an array of numbers.

Thank you guys, “let y =0;” worked for first test.
And “newdoors.push(y+1);” worked for second test.
Have a great day,

1 Like