Catch Off By One Errors When Using Indexing

Catch Off By One Errors When Using Indexing
0.0 0

#1

Tell us what’s happening:
I am supposed to subtract 1 from length to make it a 0-based index and I tried doing that in line where it says to modify by subtracting 1 from the length. It passes all tests except

“Your code should set the terminal condition of the loop so it stops at the last index.”

I don’t know what’s going on

Your code so far


function countToFive() {
  let firstFive = "12345";
  let len = firstFive.length;
  // Fix the line below
  for (let i = 0; i <= len-1; i++) {
  // Do not alter code below 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/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/debugging/catch-off-by-one-errors-when-using-indexing


#2

There is a simpler way to set the condition to stop before you are out of bounds than subtracting 1.
hint


#3

That’s very helpful, I can’t believe I hadn’t thought of that.