Profile Lookup ( Basic javascript quiz)

I’ve been trying to make this code work to no avail, can you guys please help me check it out?

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

// Change these values to test your function
console.log(lookUpProfile("Kristian", "likes"))

The “else if” statement keeps being executed even when the condition of the “if” is met.:sneezing_face::sneezing_face:

What makes you think this? In which test case do you think this is happening?

This is what i get when using the “else if” statement.


When i commented out the else if statement

only that its not solving the quiz problem and i feel thats all i require

Try your code with the various test cases in this tool:
http://pythontutor.com/javascript.html

The code with the if else statement, so you can see what happens
Try with maybe these function calls, and see what your code does:

lookUpProfile("Sherlock", "number"); // should return "0487345643"
lookUpProfile("Jacob", "likes"); // should return "No such contact"
lookUpProfile("Kristian", "address"); // should return "No such property"

Afterward if you need ask again

1 Like

Before getting into why your if statement logic is not correct, you also need a typo in the following line:

    return "No such contanct"

OK, now that’s out of the way, I will explain what is wrong with your current if statement logic. First, remember as soon as your code executes a return statement, the function exits and does not return to finish a loop.

In the test case of lookUpProfile(“Kristian”, “lastName”), your for loop starts with i = 0, so contacts[0].firstName is “Akira”. Your if statement checks if “Akira” is equal to name. Since name is “Kristian”, this makes your if statement evaluate to false. This is why your else if’s return statement executes and stops the loop and returns “No such contanct”.

Like Randell already mentioned. The return statement kicks you out of the loop. You can fix it by introducing a variable above the loop, assign the matching value to it inside the loop and return the variable after the loop. Something like this:

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

// Change these values to test your function
console.log(lookUpProfile("Kristian", "likes"))

@willyblack What @NicolasBG87 suggests is closer to the correct answer, but does not solve all your issues.

Think about it for a minute. If the first if statements evaluates to false because contacts[i].firstName === name is not true, then the else if is definitely going to evaluate to true, but that does not help you at all. Why? Because just now match is always going to contain a value based off the very last contact in the contacts array. You only need to return “No such contact” once you have been through the entire contacts array and no first names matched.

@NicolasBG87 I am sure you will figure out your mistake here, but please do not try to post a full working solution. Just stick to hints and examples. Thank you.