Solution is wrong?

Tell us what’s happening:

Your code so far


function countToFive() {
let firstFive = "12345";
let len = firstFive.length - 1;
// Only change code below this line
for (let i = 0; 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.138 Safari/537.36.

Challenge: Catch Off By One Errors When Using Indexing

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

So the problem tells me to “Fix the two indexing errors in the following function so all the numbers 1 through 5 are printed to the console,” however the solution that works doesnt print numbers 1 - 5 in the console, it prints 1-4… I had a solution that printed 1-5 and it was marked wrong by the program. Am i missing something?

You only fixed one of the indexing errors. The declaration of length is strange.

1 Like

Edit: I think its because i changed the len = to have a -1 to solve the issue rather than changing it in the for loop

You solution doesn’t match the guide solution. You’re right that the -1 is causing the issue.

Off by one errors can cause a headache.

1 Like