# Loop - increment

**URL:** <https://forum.freecodecamp.org/t/loop-increment/463307>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [June 3, 2021, 6:37pm UTC](https://forum.freecodecamp.org/t/loop-increment/463307 "2021-06-03T18:37:52Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![WebQuestions](https://avatars.discourse-cdn.com/v4/letter/w/8e7dd6/32.png) [@WebQuestions](https://forum.freecodecamp.org/u/WebQuestions)\
**Post date:** [June 3, 2021, 6:37pm UTC](https://forum.freecodecamp.org/t/loop-increment/463307/1 "2021-06-03T18:37:52Z")

</div>

Dear Friends,

I am wondering if I correctly understand how in below example increment (I) works. As I understood when (i) checks name “Akira” i== 0 and when program goes down and checks “Harry” (i) increments and equals i==1 end so forth.

is my reasoning correct?

please can someone confirm if i correctly understood how i works

```
   **Your code so far**

```

```js

// Setup
var contacts = [
   {
       "firstName": "Akira",
       "lastName": "Laine",
       "number": "0543236543",
       "likes": ["Pizza", "Coding", "Brownie Points"]
   },
   {
       "firstName": "Harry",
       "lastName": "Potter",
       "number": "0994372684",
       "likes": ["Hogwarts", "Magic", "Hagrid"]
   },
   {
       "firstName": "Sherlock",
       "lastName": "Holmes",
       "number": "0487345643",
       "likes": ["Intriguing Cases", "Violin"]
   },
   {
       "firstName": "Kristian",
       "lastName": "Vos",
       "number": "unknown",
       "likes": ["JavaScript", "Gaming", "Foxes"]
   }
];

function lookUpProfile(name, prop){
// Only change code below this line
function lookUpProfile(name, prop) {
 for (let i = 0; i < contacts.length; i++) {
   if (contacts[i].firstName === name) {
     if (prop in contacts[i]) {
       return contacts[i][prop];
     } else {
       return "No such property";
     }
   }
 }

}
// Only change code above this line
return "No such contact";
}

lookUpProfile("Akira", "likes");

```

```
   **Your browser information:**

```

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

**Challenge:** Profile Lookup

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [June 3, 2021, 6:42pm UTC](https://forum.freecodecamp.org/t/loop-increment/463307/2 "2021-06-03T18:42:18Z")

</div>

> [@WebQuestions](#):
>
> ```auto
> function lookUpProfile(name, prop){
> // Only change code below this line
> function lookUpProfile(name, prop) {
> for (let i = 0; i < contacts.length; i++) {
> if (contacts[i].firstName === name) {
> if (prop in contacts[i]) {
> return contacts[i][prop];
> } else {
> return "No such property";
> }
> }
> }
> 
> }
> // Only change code above this line
> return "No such contact";
> }
> 
> ```

The code makes it look like you pasted a function you found into this function? This function won’t do anything but `return "No such contact"`.

---

<div class="post-metadata">

**Author:** ![WebQuestions](https://avatars.discourse-cdn.com/v4/letter/w/8e7dd6/32.png) [@WebQuestions](https://forum.freecodecamp.org/u/WebQuestions)\
**Post date:** [June 3, 2021, 7:05pm UTC](https://forum.freecodecamp.org/t/loop-increment/463307/3 "2021-06-03T19:05:25Z")

</div>

Yes I just copied function. But can you please explain how (i ) will increment, if i will pass (name, prop)

Thanks a lot

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [June 3, 2021, 7:06pm UTC](https://forum.freecodecamp.org/t/loop-increment/463307/4 "2021-06-03T19:06:45Z")

</div>

Currently, nothing will happen because you have a function inside of a function.

---

<div class="post-metadata">

**Author:** ![WebQuestions](https://avatars.discourse-cdn.com/v4/letter/w/8e7dd6/32.png) [@WebQuestions](https://forum.freecodecamp.org/u/WebQuestions)\
**Post date:** [June 3, 2021, 7:56pm UTC](https://forum.freecodecamp.org/t/loop-increment/463307/5 "2021-06-03T19:56:17Z")

</div>

Will below code work now??

Thanks

```auto
function lookUpProfile(name, prop) {
 for (let i = 0; i < contacts.length; i++) {
   if (contacts[i].firstName === name) {
     if (prop in contacts[i]) {
       return contacts[i][prop];
     } else {
       return "No such property";
     }
   }
 }

}
// Only change code above this line
return "No such contact";
}

lookUpProfile("Akira", "likes"

```

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [June 3, 2021, 8:10pm UTC](https://forum.freecodecamp.org/t/loop-increment/463307/6 "2021-06-03T20:10:49Z")

</div>

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [June 3, 2021, 8:15pm UTC](https://forum.freecodecamp.org/t/loop-increment/463307/7 "2021-06-03T20:15:37Z")

</div>

The logic of your code is correct but you have two errors that prevent your code from passing .

Error no1:

> [@WebQuestions](#):
>
> ```auto
> }
> // Only change code above this line
> 
> ```

Delete this extra curly brace.

Error no.2:

> [@WebQuestions](#):
>
> `lookUpProfile("Akira", "likes"`

You are missing a parenthesis at the end.

When you fix those errors then it should pass.

---

<div class="post-metadata">

**Author:** ![WebQuestions](https://avatars.discourse-cdn.com/v4/letter/w/8e7dd6/32.png) [@WebQuestions](https://forum.freecodecamp.org/u/WebQuestions)\
**Post date:** [June 4, 2021, 12:01pm UTC](https://forum.freecodecamp.org/t/loop-increment/463307/8 "2021-06-04T12:01:19Z")

</div>

Thanks a Lot, I corrected mistakes I checked and it works. Can you please explain when (I) increments?

```auto
function lookUpProfile (name, prop) {
 for (let i = 0; i < contacts.length; i++) {
   if (contacts[i].firstName === name) {
     if (prop in contacts[i]) {
       return contacts[i][prop];
     } else {
       return "No such property";
     }
   }
 }
return "No such contact";
}

lookUpProfile("Akira", "likes")
```

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [June 4, 2021, 2:12pm UTC](https://forum.freecodecamp.org/t/loop-increment/463307/9 "2021-06-04T14:12:20Z")

</div>

It’s a bit of a miracle to me how you could proceed that far in the curriculum without understanding the basics of a for loop, but here’s what’s happening, and the structure:

```auto
for (<initial expression>, <condition>, <how-to-increment>){
    <code block>
}

```

1. the `<initial expression>` is evaluated. In your case, that’s `let i=0`
2. the `<condition>` is evaluated. In your case, that’s `i < contacts.length`. If the condition is true, the `<code block>` executes, if the condiion is false, the loop is terminated
3. finally, the `<how-to-increment>` part runs. In your case, that’s `i++`, which is the same as `i = i+1`

And then it goes back to 2, to check if the condition is still true.

Read more here:

> **[Loops and iteration - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration)**
>
> Loops offer a quick and easy way to do something repeatedly. This
> chapter of the JavaScript Guide
> introduces the different iteration statements available to JavaScript.

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [June 4, 2021, 2:16pm UTC](https://forum.freecodecamp.org/t/loop-increment/463307/10 "2021-06-04T14:16:29Z")

</div>

> [@WebQuestions](#):
>
> Can you please explain when (I) increments?

I think the best way to see how your function works is to test it with one of the test cases.

Here is one example  
lookUpProfile(“Bob”, “potato”)

If `i=0` and this condition(`i < contacts.length`) is true, then we go to our first if statement.

`if (Akira === Bob)`?  
Since the answer is no then we increment `i` and `i=1`.  
Check the condition `i < contacts.length` and since it is true then you go to the if statement again.

`if(Harry === Bob)`?  
Since that is no then we increment `i` and `i=2`.

You keep repeating the process while the condition in the for loop is true.

In this case, we will go through the entire array and find that none of the first names match bob so the function will return No such contact.

Hope that makes sense!

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [June 4, 2021, 2:20pm UTC](https://forum.freecodecamp.org/t/loop-increment/463307/11 "2021-06-04T14:20:21Z")

</div>

I would highly recommended you stop looking at and copying solutions right now, because it seems to be holding back your understanding.

---

<div class="post-metadata">

**Author:** ![WebQuestions](https://avatars.discourse-cdn.com/v4/letter/w/8e7dd6/32.png) [@WebQuestions](https://forum.freecodecamp.org/u/WebQuestions)\
**Post date:** [June 4, 2021, 7:01pm UTC](https://forum.freecodecamp.org/t/loop-increment/463307/12 "2021-06-04T19:01:17Z")

</div>

Thanks For your Reply it was very helpful

---

<div class="post-metadata">

**Author:** ![Sylvant](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sylvant/32/254977_2.png) [@Sylvant](https://forum.freecodecamp.org/u/Sylvant)\
**Post date:** [June 4, 2021, 7:20pm UTC](https://forum.freecodecamp.org/t/loop-increment/463307/13 "2021-06-04T19:20:11Z")

</div>

‘`i`’ is incremented in the end of each iteration.  
This code:

```auto
for (let i=0; i<5;i++){
  
}

```

Is similar to this code:

```auto
let i=0
while(i<5){

  i++
}

```

One major difference is the iterator(`i`) in `for` loop is declared inside the loop body and stay local(it wont be recognized outside the loop), while in `while` loop, we declare the iterator outside the loop body(if we use iterator variable to run it), which makes it persists after the loop has finished(it has a global scope)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [December 4, 2021, 7:20am UTC](https://forum.freecodecamp.org/t/loop-increment/463307/14 "2021-12-04T07:20:45Z")

</div>

Closed on December 4, 2021, 7:20am UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
