Basic JavaScript - Profile Lookup

Hello, could someone please help me understand what I did wrong here? I’m having trouble passing the first three tests with the shorter code. I managed to pass them before using a longer and more tedious approach

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){
      if (contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop];
      }
      return "No such property"
    }
    return "No such contact"
  }

  /* let sumName = 0; 
  let sumProp = 0;
  for (let i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)){
      return contacts[i][prop];
    }
    if (contacts[i].firstName != name){
      sumName++;
    }
    if (!contacts[i].hasOwnProperty(prop)){
      sumProp++;
    }
  }
  if (sumName == contacts.length){
    return "No such contact";
  }
  if (sumProp == contacts.length){
    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/116.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

Remember, a return statement immediately causes the function to exit. Look at your for loop. Is there ever a time it won’t hit a return statement on the first iteration?

Sorry, but which return do you mean?

I just looked at the hint and realized that I placed the return "No such contact" incorrectly. I should have placed it right before the closing brace of the function. This makes me more confused. Doesn’t it cause the function to always return ‘No such contact,’ regardless of the condition?

In your For loop, you start by checking if the first contact’s firstName is the name in question. If not, you return "No such contact". You didnt give to for loop a chance to check through the rest of the contacts. The for loop will never reach the second contact.

Cut the return "No such contact" and place it outside the for loop. (Cut and paste below the } bracket below it.)

Also, if you get stuck like that, use console.log() to see what your code is doing. Some people hate it, but its your code. You can go wild:

function lookUpProfile(name, prop) {
  // Only change code below this line
  console.log("New search starting.....")
  for (let i = 0; i < contacts.length; i++){
    console.log("Looking at " + name + " and prop " + prop)
    if (contacts[i].firstName == name){
      if (contacts[i].hasOwnProperty(prop)) {
        console.log("Returned " + contacts[i][prop])
        return contacts[i][prop];
      }
      console.log("Returned No such property")
      return "No such property"
    }
    console.log("Returned No such contact");
    return "No such contact"
  } 
  // Only change code above this line
}

Not exactly. That return "No such contact" is arrived at only after the for loop has run through all elements of the array, and none of the conditions held in the for loop is met.

But it is inside the for loop? It will be reached in every iteration…

Yes, their original code had this flaw,

Looking here, it seems that they corrected it though.

Your idea of using console.log() to debug code is a good one

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