Why its not working ? help!

Tell us what’s happening:
Describe your issue in detail 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
for (var i=0; i<contacts.length; i++){

if ( contacts[i].firstName === name 
&& contacts[i].lastName === prop
|| contacts[i].number === prop
|| contacts[i].likes === prop){
  return contacts[i][prop] || "No such property";
}
return "No such contacts";
}
// Only change code above this line
/* 
function lookUpProfile(name, prop){
for (var i = 0; i < contacts.length; i++) {
  if(contacts[i].firstName === name) {
    return contacts[i][prop] || "No such property";
  }
}
return "No such contact";
} */
}
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/99.0.4844.51 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

What Are You Running it On?

the built in editor :slightly_smiling_face:

Okay Let Me Analyse your code

1 Like
contacts[i].lastName === prop

You are comparing the key to the value.

prop is the key
contacts[i].lastName, or contacts[i][prop] is the ‘value’.


const user = {
  firstName: 'Akira',
  lastName: 'Laine',
  number: '0543236543',
  likes: ['Pizza', 'Coding', 'Brownie Points'],
};

const prop = 'firstName';

console.log(user.firstName); // Akira
console.log(user[prop]); // Akira

console.log(prop); // 'firstName'
console.log(user.firstName === prop); // false

i think u have error in if else statement. the task is :
The function should check if name is an actual contact’s firstName and the given property ( prop ) is a property of that contact.

If both are true, then return the “value” of that property.

If name does not correspond to any contacts then return the string No such contact .

If prop does not correspond to any valid properties of a contact found to match name then return the string No such property .

there has “given property (prop) is a property of that contact” text. so the if else statement need to check are the contacts have same property in given props? how do u check it? use hasOwnProperty(). if u using contacts[i].likes === prop it just checking are in contacts[i].likes has “likes” value?

and just my suggest. use else to return “No such property” instead || operator in return. maybe

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