What's wrong with my code?It doesn't work!

What's wrong with my code?It doesn't work!
0.0 0

#1

I can’t figure out what is the problem.Please guide me.

//Setup
var 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(firstName, prop){
// Only change code below this line
for ( var i = 0 ; i < contacts.length; i++) {
  if (firstName == contacts[i].firstName) {
    if (contacts[i].hasOwnProperty(prop)){
      return contacts[i].prop;
    } else {
      return "No such property";
    } 
    
    } else {
      return "No such contact";
  }
}
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");

#2

You return "No such contact" too early.


#3

There is still bug bro.

function lookUpProfile(firstName, prop){
// Only change code below this line
for ( var i = 0 ; i < contacts.length; i++) {
  if (firstName == contacts[i].firstName) {
    if (contacts[i].hasOwnProperty(prop)){
      return contacts[i].prop;
    } else {
      return "No such property";
    }   
  }
}
  return "No such contact";
// Only change code above this line
}


#4

contacts[i].prop This line will look for a property called prop.


#5

:confounded: I’ve found the bug.The problem is with accessing the object’s property.
We can’t use the dot if we don’t know the exact name of the property.

This won’t work.

This will work. Anyway thank you so much bro :yum:


#6

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.


#7

Thanks! I was trying to understand the difference.