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);
}}
console.log(getFinalOpenedDoors(100))
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)
}
console.log(getFinalOpenedDoors2(100))
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:
https://www.freecodecamp.org/learn/coding-interview-prep/rosetta-code/100-doors