Rosetta Code - 100 Doors challenge

Rosetta Code - 100 Doors challenge
0

#1

I’m working on the job interview prep section and I’m stuck on the first Rosetta Code question: 100 Doors.

What is wrong with my code?

function getFinalOpenedDoors (numDoors) {
  var opened = false;
  var iterator = 0;
  var results = [];

  for(var i = iterator; i <= numDoors.length; i + iterator) {
    if(opened) {
      opened = false;
    }
    else {
      opened = true;
      results.push[i];
    }
    iterator++;
  }
  return results;
 
}



#2

Your for loop refers to numDoors.length. numDoors is a number and numbers do not have length properties. This means numDoors.length evaluates to undefined. Since iterator is 0, then in the for loop i starts at 0 and then your for loop condition evaluates 0 <= undefined which is false. This means your for loop never executes and you return an empty array instead of the correct array.


#3

FWIW there’s a very performant way to do this problem, which would probably be worth some kudos in an interview

If you need an additional hint let me know, but I suspect you may see the solution from doing the first 10 doors by hand…


#4

this is were you messing up i <= numDoors.length;

i think it should be i <= iterator.length;


#5

Hello,
I am bit confused about Rosetta Code. In which programming language can we write solutions?
I tried in JavaScript, but when I write
console.log("Hello World!");
I don’t get nothing printed on my console. I need this in order to be able to verify my steps.

Thank you for help.


#6

It’s javascript.

If console.log is not working in the editor, you can try a different online editor like this.


#7

I made the assumption that these challenges were like some of the Learn You Node challenges where there is some code being passed to you behind the scenes i.e. they are taking care of the array itself. It appears that isn’t the case.


#8

thank you. I wish Javascript would work though directly here, like it was in React turorial for example.