Execution of i inside for loops with an if statement

Hello,

Quick question but we havent done a for loop with an if statement so I was wondering when does the 'i' increase from 0 to 1 and 1 to 2 and so on? After the initialization of 0 with a name of Kristian, which is contacts[3], does it execute i++ after the initialization if the if statement is false?

  **Your code so far**

// Setup
const 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

for (var i = 0; i < contacts.length; i++) {
  if(contacts[i]["firstName"] === name) /* Iterates i by one if false */ {
    return contacts[i][prop] || "No such property";
  }
}
return "No such contact";
}
// Only change code above this line

var data = lookUpProfile("Kristian", "likes");

console.log(data);

  **Your browser information:**

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

Challenge: Profile Lookup

Link to the challenge:

  1. The first time it will initialize i (with var i = 0)
  2. It will check the condition, i < contacts.length. If it is false, the for loop is over. If it is true, we will continue.
  3. The code block is run.
  4. The final expression (i++) is run.
  5. Return to step 2.

It will keep doing that until the condition is false, or until you break the flow with a break statement or something else (throwing an exception, etc.)

does it execute i++ after the initialization if the if statement is false?

No, only at the end of the code block.

2 Likes

When I am unsure about something, I like to find a way to test it.

const initialize = () => {
  console.log('initializing')
  return 0
}

const condition = i => {
  console.log(i, 'condition')
  return i < 5
}

const final = i => {
  console.log(i, 'final, about to increment to', i + 1)
  return i + 1
}

console.log('before loop')
for (let i = initialize(); condition(i); i = final(i)) {
  console.log(i, 'code block')
}
console.log('after loop')

By using functions in those sections, we can see when they are happening. I get this for a log out:

before loop
initializing
0 ‘condition’
0 ‘code block’
0 ‘final, about to increment to’ 1
1 ‘condition’
1 ‘code block’
1 ‘final, about to increment to’ 2
2 ‘condition’
2 ‘code block’
2 ‘final, about to increment to’ 3
3 ‘condition’
3 ‘code block’
3 ‘final, about to increment to’ 4
4 ‘condition’
4 ‘code block’
4 ‘final, about to increment to’ 5
5 ‘condition’
after loop

1 Like

This part confuses me because lets say it runs with the initialization of 0. But I test with

var data = lookUpProfile("Kristian", "likes");

console.log(data);

The code runs the block of code and obviously the if statement is false since the initialization’s first name is Akira, not Kristian. I would assume since its false, it would follow the normal flow and go to the return "No contact found" like an else statement, instead of incrementing i by one each time at the end of the block of code.

This part confuses me because lets say it runs with the initialization of 0.
Yes. We don’t have to assume that. It’s written in the code.

I would assume since its false, it would follow the normal flow and go to the return "No contact found"

I don’t see that in the code.

Again, when in doubt, just investigate:

function lookUpProfile(name, prop) {
  for (var i = 0; i < contacts.length; i++) {
    console.log('*** for loop running, i =', i, 'and contact is', contacts[i].firstName)
    if(contacts[i]["firstName"] === name) /* Iterates i by one if false */ {
      console.log('first name matches', contacts[i][prop])
      console.log('prop is', contacts[i][prop])
      return contacts[i][prop] || "No such property";
    } else { console.log('this name did not match, continuing with loop') }
  }
  console.log('for loop done, no match found')
  return "No such contact";
}