Help I'm stuck at this challenge

Tell us what’s happening:
I have no idea how to return the final answer in this function.

  **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
let result = "";
for(let i=0; i < contacts.length; i++) {
if(contacts[i].firstName === name) {
  result = "No such contact";
}else if(contacts[i].hasOwnProperty(prop) === false) {
  result = "No such property";
}else {
  result = 
}
}
return result;
// 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/98.0.4758.87 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

Remember that when a function hits a return statement, the function exits. Because you have this:

Your code will only ever look at the first item in contacts. If it doesn’t match the name parameter, the function will return “No such contact” and that’s it.

1 Like

Hey.
Let’s break this problem down into smaller pieces.
So, the for loop iterates over the contacts array to see if any of the firstNames matches the name.
Now, if there is a firstName that matches name, the next step is checking if prop matches any property of the object that has the matching firstName. So, one of two things can happen here:

  1. There is a property that has the same name as prop. In this case return the value of that property.
  2. There isn’t any property that matches prop, in which case you return 'No such property'.

But hey, what if the for loop finished looping over the contacts array and didn’t find any matching firstNames? In that case, return 'No such contact'.
I hope this is helpful.
Cheers.

1 Like

this updated code passes 3 out 6 tests, can anyone tell what I am doing wrong here.

  **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
var result = ""
for (let i=0; i < contacts.length; i++) {
if(contacts[i].firstName === name) {
  if(contacts[i].hasOwnProperty(prop) === true) {
    result =  contacts[i][prop];
  }else {
    return "No such property";
  }
}else {
  return "No such contact";
}

}
return result;
// Only change code above this line
}

console.log(lookUpProfile("Akira", "likes"));

Hello, I think this the problem. Dot notation does not work with variables. Try bracket notation, here. Hope this helps. Good luck.

still having same problem

Which tests are not passing?

these 3 aren’t passing.

lookUpProfile("Kristian", "lastName") should return the string Vos

lookUpProfile("Sherlock", "likes") should return ["Intriguing Cases", "Violin"]

lookUpProfile("Harry", "likes") should return an array

Why do you need the for loop to include else statements. Shouldn’t the for loop first finish before these statements come into play?

what i thought is -
first its going to check this statement

if true, it’s going to check this nested statement

if first statement is false, it will return this

if second statement (nested one) is false it will return this

I have to return “No such property” statement after checking, if this isn’t the right way plz help me here.

Sorry, this is what I was trying to say.:grimacing:

1 Like

Let’s analyze why this one happens. The other two follow a similar logic.

So, the for loop reaches this object, which is the 3rd object in the contacts array(i===2).

Then, the if statement checks if contacts[i].firstName === name, and those 2 are indeed equal, so we proceed to the nested if statement.

The nested if statement checks if the contacts[i] object has the 'likes' property. And it does, so result gets the value ["Intriguing Cases", "Violin"].

Normally, the function should stop here. But it doesn’t.
There’s still one more object in the array, so the for loop keeps going to i===3. It does the first if statement again, which is false this time, so it jumps to the else branch, which says return "No such contact";. This is the first time the function finds a return statement, so it exits here and returns “No such contact”, which isn’t what you want.

Modify the function in such a way, that it stops immediately after it finds the answer you want. (Spoiler: You don’t really need that variable).
Hope i was helpful.

EDIT: After taking a second look, your function will exit right away if name doesn’t match the 'firstName' of the first object in the contacts array.
return "No such contact"; only after you checked all the objects in contacts.

2 Likes