Tell us what’s happening:
just thought i would let people know console log statements for debugging purposes can actually cause your code to fail here. i treated doors as objects and assigned them to a list and the went through the list closing/ opening doors this almost worked fine except that the array stopped adding items at around 40 doors. i couldnt figure this out originally so i moved my log statement outside of the loop to make sure that it wasnt an error with what as being logged all of a sudden the whole script was working perfectly. I can only guess that this is some kind of memory protection to prevent just crap tons of log statements from spamming the console. anyway i hope this helps someone.
Your code so far
function Door(i){
this.state = false;
this.number = i;
this.toggle = function(){
this.state = !this.state;
}
}
function getFinalOpenedDoors (numDoors) {
var doorHold = [];
for (var i = 1; i < 101; i++){
doorHold.push(new Door(i));
/*console.log(doorHold) this was the placement of the original log statement*/
}
console.log(doorHold)
var count = 1;
while (count <= 100){
for (var i = count - 1; i < doorHold.length; i+=count){
doorHold[i].toggle();
}
count++;
}
var openDoor = [];
for (var i = 0; i < doorHold.length; i++){
if (doorHold[i].state){
openDoor.push(doorHold[i]);
}
}
openDoor.map(function(x, i){
openDoor[i] = x.number;
})
console.log(openDoor);
return openDoor
}
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/69.0.3497.81 Chrome/69.0.3497.81 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/rosetta-code/100-doors/