Rosetta Code: 100 doors

Rosetta Code: 100 doors
0

#1

I try to solve this one about two days ! But still not get it . Anyone can help me with what I am missing ???

Your code so far


function getFinalOpenedDoors (numDoors) {
  // Good luck!
  var doorState = {};
  var result = [];
  for(var i = 1; i <= numDoors; i++) {
    for(var j = i; j <= numDoors;j += i) {
      if(doorState[j] == null) {
        doorState[j] = false;
      }
      doorState[j] = !doorState[j]
    }
  }
  for(var j in doorState) {
    if(doorState[j]) result.push(j);
  }
  return result;
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/rosetta-code/100-doors


#2

Uhm, it has something to do with the divisors right?
I mean, the number of passes through the door depend on how many integer divisor the number has: a prime number is divisible by 1 and by itself, so you will pass, let’s say, through the port 17 twice (once when you make the first pass, once when you make the 17 pass).

In other terms the function should evaluate the number of integer divisor for each value from 1 to 100: if it’s even the door is closed ( the doors start closed, toggled a even number of times it ends up closed), if it’s odd is opened.

Good luck! :slight_smile: