Profile Lookup explanation of my code

Tell us what’s happening:

My test is not passing for the No such property, I don’t understand why…My thought was its gonna look for an if condition inside the for loop first and when that condition is not met, the block will go to the second if block outside for loop and finally to the last if statement. Please help me understand what is wrong with the code… I do already know how to do the solution, but I want to understand what’s going on my original code so that I get a clear understanding of how the block of code works. Also, I tried to solve the solution by adding three different for blocks to surround each if statements but did not work as I was expecting. Thank you for your help in advance :slight_smile:

Your code so far

function lookUpProfile(name, prop){

// Only change code below this line

for (var i = 0; i < contacts.length; i++){

if (name === contacts[i]["firstName"] && contacts[i].hasOwnProperty(prop)){

return contacts[i][prop];

}

}

if (name !==contacts["firstName"]){

return "No such contact";

}

if (name === contacts["firstName"] && contacts.hasOwnProperty(prop) == false){

return "No such property";

}

// Only change code above this line

}
// Later I tried this code as well 
function lookUpProfile(name, prop){

// Only change code below this line

for (var i = 0; i < contacts.length; i++){

if (name === contacts[i]["firstName"] && contacts[i].hasOwnProperty(prop)){

return contacts[i][prop];

}

}

for (var j = 0; j < contacts.length; j++){

if (name !==contacts[j]["firstName"]){

return "No such contact";

} }

for (var k = 0; k < contacts.length; k++){

if (name === contacts[k]["firstName"] && contacts[k].hasOwnProperty(prop) == false){

return "No such property";

}

}

// Only change code above this line

}

```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
for (var i = 0; i < contacts.length; i++){
if (name === contacts[i]["firstName"] && contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}
}
 if (name !==contacts["firstName"]){
    return "No such contact";
} 
 if (name === contacts["firstName"] && contacts.hasOwnProperty(prop) == false){
    return "No such property";
}


// Only change code above this line
}

// Change these values to test your function
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/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

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 easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

In your first code, this:

if (name !== contacts["firstName"]) {
    return "No such contact";
}

Will always be true unless name is undefined. The way your data is structured, you have an array of contacts, so you need the index e.g. contacts[1]["firstName"]. Even if you were to try to use the index though, it isn’t inside a loop or anything so it would always be testing for the same contact.

You have the same mistake in the “no such property” test, but it doesn’t matter cause it never gets there.

Your second iteration, you fixed that problem. Yay you! but you have another one:

for (var j = 0; j < contacts.length; j++) {

  if (name !== contacts[j]["firstName"]) {

    return "No such contact";

  }
}

Here, as soon as the loop comes up with a name that doesn’t match name it’s gonna say “no such contact” So if the name of contact 0 is “Fred” and name is “Mary”, ti doesn’t matter that contact 3 is “Mary”. as soon as the loop hits the first contact it says “Nope. No such contact”.

Similar problem with “No such property” but again it doesn’t matter cause it’s never gonna get there.

This all may be predicated on a misunderstanding of how return works. return does not just stop the loop and go onto the next test, return stops the whole function and gives back to the outside world whatever is to its right.

OMG, I just understood the problem…You are awesome…Thank you so much…My brain is getting bigger now :smiley:

1 Like