Profile LookUp Challenge Headache

Tell us what’s happening:

I’ve been stuck on this for a while now guys.
I really want to take my time to understand the concept.
Anyone care to explain?

Your code so far


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

Challenge: Profile Lookup

Link to the challenge:

you need to understand one thing


for (var i = 0; i < contacts.length; i++){
   ...
   return "No such contact"
}

the return statement there means that the loop will never go after the first iteration as once the return statement execute the function stops

1 Like

You need to properly close down strings
There also want to be some space with the else

1 Like

I’ve checked and its still not passing.

what’s your new code?
we can’t know what you changed, so if you changed something and you still need help you need to show the new version of the code

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

 lookUpProfile("Sherlock", "likes"); 

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

my previous post still stand

Hi ieahleen,
It took quite a while to grasp, but i finally understood that I had to allow all iterations pass and return the “No such contact” as the final function outside of the loop.

Thank you.

2 Likes

if you have questions or don’t understand what something is said, please say so and ask again.

If you don’t say anything it is impossible to understand what you need explained

anyway, glad you did it. Happy coding!

2 Likes