Loop - increment

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**

// 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:

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".

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

Thanks a lot

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

1 Like

Will below code work now??

Thanks

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"

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.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

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

Error no1:

Delete this extra curly brace.

Error no.2:

You are missing a parenthesis at the end.

When you fix those errors then it should pass.

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

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")

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:

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:

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!

1 Like

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

Thanks For your Reply it was very helpful

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

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

Is similar to this code:

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)

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