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

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

#1

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.


#2

To what zero are you referring?


#3

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.


#4
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.


#5

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


#6

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


#7

I am going to let you answer some questions to see what you do not understand and then point you to the correct FCC challenge for you to review

  1. What type of data structure is contacts?

  2. What kind of data is stored in contacts?


#8
  1. contacts is an array.

  2. objects are stored in the array.


#9
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.


#10

You changed which code you were originally asking about, but that is ok.

I took your most recent version above and put some comments and console.log statements in so you can see what is happening. I highly recommend you start using console.log statements when reviewing other code. It will help you see what is happening during script execution.

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;
} 

Run the above code with a couple of test cases. Your code still fails in the case where the firstName argument does not match any object’s firstName property.


#11

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


#12

It displays what you specify to the browser’s console (Ctrl+Shft+J in Chome).


#13

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


#14

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?


#15

You should learn how to use the browser’s console to look at your data. Codepen has a console screen, but it does not always show everything it should. For most of the FCC JavaScript challenges, I liked using https://repl.it because I can choose different languages and see the console real-time. It does not require an account unless you want to save your work.


#16

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


#17

Can you share the repl.it link? You can click the share button and copy/paste that link directly into a reply.


#18

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

#19

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


#20

You did not paste the link. Just edit the above post to add the link.