Basic JavaScript - Profile Lookup

Seems like my code should work but i dont think the has own property statements are working correctly because the tests that involve them fail. Anyone have an idea what is wrong with them?

Your code so far

// Setup
const 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 (let cycle = 0; cycle < contacts.length; cycle++) {
if (name === contacts[cycle].firstName  && contacts[cycle].hasOwnProperty(prop) == true) {
  return contacts[cycle].prop;
} else if (name !== contacts[cycle].firstname) {
  return "No such contact";
}else if (name === contacts[cycle].firstname && contacts[cycle].hasOwnProperty(prop) == false) {
  return "No such property";}
  // Only change code above this line

lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

A return statement immediately stops the function call.

Also, capitalization matters in variable and property names.

Also, your braces are misleading.

I removed my returns and checked all the variable but it still doesnt work

 let x = 0
  for (let cycle = 0; cycle < contacts.length; cycle++) {
if ((contacts[cycle].firstName === name) && (contacts[cycle].hasOwnProperty(prop))) {
  x = contacts[cycle][prop];
} else if (name !== contacts[cycle].firstName) {
  x = "No such contact";
}else if ((contacts[cycle].firstName === name) && (contacts[cycle].hasOwnProperty(prop)) == false) {
  x = "No such property";}
} return x
  // Only change code above this line

You need to return at the right point. Do you need to keep searching if you found the person?

then it seems like the original returns are correct?

Nope. The one I quoted is a problem

You were right i got rid of the last if statement and put the return outside of the loop and it works but i dont understand why it was stopping the loop it seems that the return for the last statement would only be executed if the name was not found so why would it affect the outcome of the first if statement?

What happened when the first person didn’t have a matching name?

Yeah i see. I guess putting that statement in was completely unnecessary anyway but is there a way to write an if statement that would use the return only if the name wasnt found. Also i guess it has to loop through one time before it does anything ? Because the first test was a name that was the first on the list of the object. Thanks for the help tho i wrote the code in a few minutes then spent hours trying to figure out what was going wrong.

It’s the interaction between the for and if you have to keep in mind when you combine them

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.