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:

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

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