# freeCodeCamp Challenge Guide: Catch Off By One Errors When Using Indexing

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-catch-off-by-one-errors-when-using-indexing/301189>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [August 3, 2019, 5:47am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-catch-off-by-one-errors-when-using-indexing/301189 "2019-08-03T05:47:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [August 3, 2019, 5:47am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-catch-off-by-one-errors-when-using-indexing/301189/1 "2019-08-03T05:47:55Z")

</div>

# [Catch Off By One Errors When Using Indexing](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/debugging/catch-off-by-one-errors-when-using-indexing)

* * *

## Problem Explanation

Due to the way JavaScript indexes work `firstFive` has **five elements** but they are indexed from **0 to 4**!

```javascript
console.log(len); // 5
console.log(firstFive[0]); // 1
/**/
console.log(firstFive[4]); // 5
console.log(firstFive[5]); // undefined

```

That should give you enough to grasp the limits of `firstFive`. Direct your attention to the loop. What does it do? You could try debugging it to find out!

**Debugging**

You are given this code:

```javascript
for (let i = 1; i <= len; i++) {
  console.log(firstFive[i]);
}

```

To debug this piece of code, use `console.clear()`. What would be the best place for it? The answer is right before the `for` statement!

```javascript
console.clear();
for (let i = 1; i <= len; i++) {
  console.log(firstFive[i]);
}

```

Console output:

```auto
  Console was cleared.
  2
  3
  4
  5
  undefined

```

**Analysis**

Examine the output. Under these conditions the loop first prints the element positioned at 1… which is 2! It also tries to print the element indexed at 5 which is `undefined`.

This can be considered the point of this challenge. Keep `console.log()` and `console.clear()` present. They will help you understand how your code works.

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```javascript
> function countToFive() {
> let firstFive = "12345";
> let len = firstFive.length;
> // Fix the line below
> for (let i = 0; i < len; i++) {
> // Do not alter code below this line
> console.log(firstFive[i]);
> }
> }
> 
> ```
> 
> #### Code Explanation
> 
> - The most straightforward way to fix this is to alter the for() conditions.
> - Make `i` start at 0. Also the loop **should not** be executed for i == 5. In other words, the relationship between `i` and `len` should be `false` when i == 5. That can be achieved by using `i < len` (Is 5 \< len? false, and the loop won’t be executed!).
> 
> #### Relevant Links
> 
> - [For statements challenge at FreeCodeCamp](https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops)
> - [For statements at MDN web docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration#for_statement)

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 8, 2025, 3:58pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-catch-off-by-one-errors-when-using-indexing/301189/2 "2025-01-08T15:58:57Z")

</div>

Closed on January 8, 2025, 3:58pm UTC


