Tests are probably bugged (100 doors)

Tell us what’s happening:
tests are not passing for recursive solution although the result is exactly the same, array of squares of 1-9 which is logical because they have 3 dividors (1, sqrt and themselves) and everything else is separetad into two other numbers (and 2 more factors don’t change the door situation)

Your code so far

var arr = [];
for (let i = 1; i <= 100; i++) {arr.push(i);}

function getFinalOpenedDoors(numDoors, iter = 2) {
if (iter == 101) {return arr.filter(Boolean);}
else {
for (let i = 0; i < arr.length; i++) {
if ((i+1) % iter === 0) {
if (arr[i] !== false) arr[i] = false;
else arr[i] = i+1;
return getFinalOpenedDoors(numDoors, iter+1);

function getFinalOpenedDoors2(numDoors) {
var arr = [];
for (let i = 1; i <= numDoors; i++) {arr.push(i);}

for (let i = 2; i <= numDoors; i++) {
for (let j = 0; j < arr.length; j++) {
if ((j+1) % i == 0 && arr[j] != false) arr[j] = false
else if ((j+1) % i === 0 && arr[j] == false) arr[j] = j + 1;
return arr.filter(Boolean)

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.183 Safari/537.36.

Challenge: 100 doors

Link to the challenge:

also, can i put the global arr and for inside the recursive function without forcing it to reset arr?

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.


var myGlobal = [1];
function returnGlobal(arg) {
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  return myLocal;
} // reliable - always returns an array of length 2

so you suggest me to put arr in the recursive function (at least i think) but will it reset it every time it is run or make the execution time much longer?

you can’t have global variables.

but you can have a local variable, and then an helper function that is recursive that change that variable

1 Like

oh never mind just put the recursive function in a function

function getFinalOpenedDoors(num)
arr = []
for loop
recursive function that changes arr
return recusive function value