Profile Lookup Challenge - Need Help

Profile Lookup Challenge - Need Help
0.0 0

#1

Not sure why this isn’t working. Please help.

Code so far:

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 (contacts[i]["firstName"] === name && contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    }
    else if (contacts[i]["firstName"] != name) {
      return "No such contact";
    }
    else if (!contacts[i].hasOwnProperty(prop)) {
      return "No such property";
    }
  }
// Only change code above this line
}

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

#2

Here you can see the output I am getting.


#3

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a 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.


#4

It fails the first tests, because your code only makes it through one iteration of the for loop before returning something. Once a return statement is executed, the function immediately exits.

Walk through your code for the first test and you will see in the first test case of lookUpProfile(“Kristian”, “lastName”), that your code returns “No such contact”.


#5

In the first if statement “firstName” === “Kristian”
and there is a property called “lastName”

So the if statement should evaluate to true && true.

I don’t see why it returns “No such contact”.


#6

I see it now, after staring at it for a while. Very confusing to me, but I get it.
Basically my code is setup to return some value the first time through no matter what and therefore the for loop is essentially useless.