What is the explanation for the For Loop in the profile Lookup exercise?

Continuing the discussion from freeCodeCamp Challenge Guide: Profile Lookup:

Here are the instructions:

Profile Lookup
We have an array of objects representing different people in our contacts lists.

A lookUpProfile function that takes firstName and a property (prop) as arguments has been pre-written for you.

The function should check if firstName is an actual contact’s firstName and the given property (prop) is a property of that contact.

If both are true, then return the “value” of that property.

If firstName does not correspond to any contacts then return “No such contact”

If prop does not correspond to any valid properties then return “No such property”

This is the solution:

for (var x = 0; x < contacts.length; x++){
    if (contacts[x].firstName === firstName) {
        if (contacts[x].hasOwnProperty(prop)) {
            return contacts[x][prop];
        } else {
            return "No such property";

I don’t understand how contacts.length matches with the other variable to allow you to look up a certain property and cycle through it. Can someone please explain what part the for loop has and what each part of it means (this includes the following - ([initialization]; [condition]; [final-expression])

More specifically, how do I know where to initiate the sequence? Does zero mean the sequence starts at firstName, for example?
What condition is it measuring as true or false
What is being plussed by 1 (i++)?

I really understood the numerical experssions (like add the odd numbers) but the word problems are totally different.

For var x = 0; x<contacts.length; x++)

What does this mean in reference to the lookup data?

Oh and here is an online javascript editor.
online javascript editor

I am going to take a break because I’ve been working on this all day today.
but I will check back in 12 hours or so.

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"]
    }

Above is the word problem in the code editor. I am still going away for a bit and will be back tomorrow.

1 Like

I’m back! Please let me know if you need any clarification.

1 Like

I was just curious if you had time to look over my question.

  1. contacts is an array.

  2. objects are stored in the array.

function lookUpProfile(firstName, prop){
    var value;
    for (var i = 0; i < contacts.length; i++){
      if((contacts[i].firstName === firstName) && 
         (contacts[i].hasOwnProperty(prop))){
        value = contacts[i][prop];
        break;
    }
    if (contacts[i].hasOwnProperty(firstName) === false){
      value = "No such contact";
    }
    if (contacts[i].hasOwnProperty(prop) === false) {
      value = "No such property";
    }
    }
    return value;
    } 

I’m trying to walk through this code… I understand the concepts by themselves but it is different understanding a word problem.

if((contacts[i].firstName === firstName) && 
         (contacts[i].hasOwnProperty(prop))){
        value = contacts[i][prop];
        break;
    }
    if (contacts[i].hasOwnProperty(firstName) === false){
      value = "No such contact";

Is this saying that if the letters of the objects match the letters of the first name, the search will proceed? and then it goes on to other things. I don’t understand the .properties part too, but one thing at a time I guess.

What does console.log exactly do? It just tells me what has been executed, correct?

So is it like sticky notes to yourself? Also, do you have a javascript editor that works when you run it? I’m having trouble adjusting the codepen.io javascript settings to allow what you sent me to actually run

But unlike sticky notes, it appears as part of the program but does not cause anything to change. Maybe more like a whiteboard (like notes) that you erase, like speech notes?

What would allow the code to run? I’m kind of stuck.
I posted it in repl.it and it does not work.

I think he added too much stuff… in my opinion.

This is the solution from the answer page on free code camp (I got confused). This is what I originally posted to you.

    if (contacts[x].firstName === firstName) {
        if (contacts[x].hasOwnProperty(prop)) {
            return contacts[x][prop];
        } else {
            return "No such property";
        }
    }
}
return "No such contact";

Here is the link, but I think he got confused with a switch statement while running an if then statement.

mixing switch code with the if statement
FCC simple answer

I posted the link.Please feel free to check it. Can I start a new thread, I’m getting confused.

what would that look like?

I know this is alot but I included the entire screen.

IT includes all the information I see.
Scroll to the bottom. I dont think calling the function was an issue because the function was called.

//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(firstName, prop){
// Only change code below this line
function lookUpProfile(firstName, prop){
  var value;
  for (var i = 0; i < contacts.length; i++){
    console.log(contacts[i].firstName === firstName, contacts[i].hasOwnProperty(prop));
    if((contacts[i].firstName === firstName) && (contacts[i].hasOwnProperty(prop))){
      console.log('firstName of object matches firstName argument and object has a property with the same name as the value of prop');
      value = contacts[i][prop]; // assign the value of the object's property with name equal to the value of prop to the value variable
      break; // stop searching because you found a match
    }
    if (contacts[i].hasOwnProperty(firstName) === false){
      value = "No such contact";
    }
    if (contacts[i].hasOwnProperty(prop) === false) {
      value = "No such property";
    }
    console.log(); // line break
  }
  return value;
} 
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");

It’s still erroring out.

Here is the link:

Again, this is the code:

Full code

There were syntax errors.

Here is the correct one:

//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(firstName, prop){
// Only change code below this line
  var value;
  for (var i = 0; i < contacts.length; i++){
    console.log(contacts[i].firstName === firstName, contacts[i].hasOwnProperty(prop));
    if((contacts[i].firstName === firstName) && (contacts[i].hasOwnProperty(prop))){
      console.log('firstName of object matches firstName argument and object has a property with the same name as the value of prop');
      value = contacts[i][prop]; // assign the value of the object's property with name equal to the value of prop to the value variable
      break; // stop searching because you found a match
    }
    if (contacts[i].hasOwnProperty(firstName) === false){
      value = "No such contact";
    }
    if (contacts[i].hasOwnProperty(prop) === false) {
      value = "No such property";
    }
    console.log(); // line break
  }
  return value;
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");

How do I read the green?

Like I posted, what is the interpretation of the green? the green arrow?

Could you use == instead of === where the equals signs are used?