Basic JavaScript - Profile Lookup

Tell us what’s happening:
I don’t know why my code doesn’t work, when it comes to names and props that exist in the array. On the other hand, it’ll return the correct output when the name or prop doesn’t exist in the array.

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 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) === 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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

Beucase you’re loop is stoping as soon as a condition is meet . So, according to the way you structured the loop it will ALWAYS run only once. Hint is to look at the return parts of your code and figure out how to go over the entire loop and then return the correct data except just looping only over the first object in the array.

Thank you for your help. I’ve changed my code to the piece below, but it doesn’t work either! I’m truly exhausted now…

// 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) {

  let answer;

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


    if( contacts[i].firstName === name && contacts[i].hasOwnProperty(prop) ) {
      answer = contacts[i][prop];
    }
    else if ( contacts[i].firstName != name ){
      answer = "No such contact"
    }
    else if ( contacts[i].hasOwnProperty(prop) === false ){
      answer = "No such property"
    }
  }

  return answer;

}

lookUpProfile("Akira", "likes");

Well, you are kinda doing the same thing now except now you take the last part of the loop. So ill try to explain to you like this, in your first try you made the loop exit on the loop’s first iteration.

On this try, your loop always finishes and always gives the answer of the last array no matter if the data you are looking for is or is not in the array . So your answer is somewhere in the middle.

Here is another hint
Do not change your answer variable if the thing you are looking for does not exist. So what that will do is that your answer variable will change if the answer exists and if not, well try and figure that one out by yourself :slight_smile: maybe another if statement in the end i don’t know :thinking: ?

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