Rosetta Code 100 doors

Tell us what’s happening:

I tried to debug this for hours, but I couldn’t figure it out. Can you tell me what is wrong?

Thanks in advance.

Your code so far


function getFinalOpenedDoors (numDoors) {
  // Good luck!
  const doors = [];
  for (let x = 0; x < numDoors; x++) {
    doors[x] = false;
  }
  for (let y = 1; y <= numDoors; y++) {
    for (let i = 0; i < numDoors; i += y) {
      if(i < numDoors) {
        doors[i] = !doors[i];
      }
    }
  }
  const toReturn = [];
  for (let key in doors) {
    if(doors[key]) {
      toReturn.push((+key) + 1);
    }
  }
  return toReturn;
}

console.log(getFinalOpenedDoors(100));

Your browser information:

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

You always start from the first door, but…

The second time, only visit every 2nd door (i.e., door #2, #4, #6, …) and toggle it. The third time, visit every 3rd door (i.e., door #3, #6, #9, …), etc., until you only visit the 100th door.

And this is a good reason to also give the challenge text or link when asking for help, without knowing that it would have been impossible to know where the issue was

It won’t let me post a link, but here is the challenge text:


There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and ‘toggle’ the door (if the door is closed, open it; if it is open, close it). The second time, only visit every 2nd door (i.e., door #2, #4, #6, …) and toggle it. The third time, visit every 3rd door (i.e., door #3, #6, #9, …), etc., until you only visit the 100th door.

Implement a function to determine the state of the doors after the last pass. Return the final result in an array, with only the door number included in the array if it is open.


It actually even gives me the wrong answer when I test it with only 2 doors there. It says that only door 2 is open, when only door 1 should be open.

Thanks in advance again

Have you changed the value st which you initialise that variable?

The issue is the same of my previous post

If any one like to play with ternary operators:

function getFinalOpenedDoors(numDoors) {
  let doors = new Array(numDoors).fill("closed");
  let arr = [];
  var j = 0;
  do {
    doors = doors.map((door, i) => {
            if ( door === "closed" && (j === 0 ? true : (j === 1 ? (i === 0 ? false : ( i === 1 ? true : ( (i+1)%2 === 0)) ) : (i === 0 ? false : ( (i+1)%(j+1) === 0 ))   ))) {
        door = "open";
      } else if (  door === "open" && (j === 0 ? true : (j === 1 ? (i === 0 ? false : ( i === 1 ? true : ( (i+1)%2 === 0)) ) : (i === 0 ? false : ( (i+1)%(j+1) === 0 ))  )   )) {
        door = "closed";
      }
      return door;
    });

    j++;
  } while (j < numDoors);

  doors.map((door, index) => {
    if (door === "open") {
      arr.push(index+1);
    }
  });
  return arr;
}

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

This is a learning site so we try to discourage people from blurting out the answers. Plus, it’s a bit of a slap in the face to the people that were trying to patiently guide the camper to the answer. If you feel you must present the answers, please be sure to blur them so people have the option of not seeing the answer.

Thanks, that is interesting. I did already find the answer earlier on. Sorry I didn’t tell you.