100 door test throw that the result is not correct

Tell us what’s happening:
100 door test throw me that the result is not correct, can somebody tell me why?

My solution:


function getFinalOpenedDoors(numDoors) {
let result =[];
for(let i=1;i<=100;i++){
 result.push(-i);
}
for(let j = 2; j<=100; j++){
 for(let i = 1; i<=100; i++){
    if(result[i]%j == 0) {
    result[i]= -result[i];
  }
 }
}
return result.filter(i=>i>0);
}

const numDoors = 100;

getFinalOpenedDoors(numDoors);
   **Your browser information:**

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

Challenge: 100 doors

Link to the challenge:

You’re failing because your answer is wrong :slight_smile:

I’m not seeing where you are visiting every other, then every third, etc

Ah, the negative sign is backwards. A bool would be easier to use

I work out it! It must be result.push(i); instead of result.push(-i);

thank you so much for your help and adwise! )

1 Like

Right, what Jeremy said…

Also, I find this a bit odd:

for(let j = 2; j<=100; j++){
  for(let i = 1; i<=100; i++){
    if(result[i]%j == 0) {

First of all, it’s a bit odd to put the j loop on the outside, not wrong per se, just against convention so it threw me for a loop (so to speak).

But my real issue is this:

for(let i = 1; i<=100; i++){

These are your indices. But your array was created with this:

for(let i=1;i<=100;i++){
 result.push(-i);
}

So, those array elements may have the values of 1-100, but those aren’t the indices of those values. I think you may be confusing the two.

1 Like

Ah ha! I knew something about the logic was bugging me, but I wasn’t sure what.

1 Like

The Spidey Sense was tingling? :wink:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.