Debugging: Catch Off By One Errors When Using Indexing: GLORY TO PYTHON!

Tell us what’s happening:
Hi. I solved this tricky challenge by writing: i = len, i < len although when I set it to i <= len it rised an error while they have the same syntax meaning. If it is not JavaScript ugliness, what could we call it?? I just don’t know why big tech companies didn’t start thinking to replace this ugly and complicated syntax language. Glory to Python!

Your code so far


function countToFive() {
  let firstFive = "12345";
  let len = firstFive.length;
  // Only change code below this line
  for (let i = 0; i = len, i < len; i++) {
  // Only change code above this line
    console.log(firstFive[i]);
  }
}

countToFive();


Your browser information:

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

Challenge: Catch Off By One Errors When Using Indexing

Link to the challenge:

The statements i = len, i < len and i <= len do not have the same meaning.

The statement i = len, i < len is not a valid loop condition. Your code does not run with this as the loop exit condition.

The statement i <= len is a logical condition that returns false when i is larger than len. Your code runs, but logs undefined after logging 5 with this as the loop exit condition.

In your code, i <= len should cause an issue because you cannot index into location 5 of the variable firstFive, as the only valid indexes are 0 - 4. This is true in Python and JavaScript.

1 Like

That’s oddly specific.

2 Likes