Rosetta Code - 100 Doors challenge

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;
  return results;

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.

1 Like

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…

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

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

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.

It’s javascript.

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

1 Like

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.

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

Hi ! just press the “run the code” button.