Basic JavaScript - Profile Lookup

Tell us what’s happening:
Describe your issue in detail here.

Code didn’t pass test - " lookUpProfile("Kristian", "lastName") should return the string Vos"
But console log shows exactly ‘Vos’ string;

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

lookUpProfile("Akira", "likes");
lookUpProfile("Kristian", "lastName");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

These return statements only apply to the callback function, not to the lookUpProfile function

2 Likes

As Jeremy said, your lookUpProfile function isn’t returning a value. If you put console.log('Kristian', 'lastName'); outside of the function, you’ll see that it’s undefined.

Tell us what’s happening:
Describe your issue in detail here.

Why lookUpProfile("Sherlock", "likes") doesn’t return ["Intriguing Cases", "Violin"]?

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(name == contacts[i].firstName && contacts[i].hasOwnProperty(prop)){
    console.log(contacts[i][prop]);
    return contacts[i][prop];
  } else if (name != contacts[i].firstName){
    return 'No such contact';
  } else if (!contacts[i].hasOwnProperty(prop)){
    return "No such property";
  }
}

  // Only change code above this line
}

lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

A return statement immediately stops a function call. This line says, in words, “if you find a contact that has a different name, give up looking and say that nobody can have a matching name”.

I’ve just pass this example. Some hint

  • You should declare variable to store answer for each loop, if you use return right in condition statement it will terminate for loop for the first loop and you can’t reach the second object in the array.
  • With the loop that results are: ans = contacts[i][prop] or ans = ‘No such property’, and break after result to terminate loop and return correct answer, other while the next loop will overwrite your correct answer

Given the name of the function lookUpProfile, I’d say they expect you to do a lookup on the array, rather than traversing every item. Your best friend here is Array.find().

So rewriting the function to do an actual lookup:

Solution redacted by moderator

@kennasoft It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

2 Likes