I can’t seem to fix this. Pls help.
MY CODE :
// 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)
return "No such contact";
else if(!contacts[i].hasOwnProperty(prop))
return "No such property";
else
return contacts[i][prop];
}
// Only change code above this line
}
lookUpProfile("Akira", "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.
You can also use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).
1 Like
Remember that when your code hits a return
statement, function execution ends. Your code will only check the first item in contacts
because you always return in the loop.
But the execution ends when i find the match for the given parameteres from the function call.
This code block is the concern Ariel was talking about.
What happens if we call the function here:
lookUpProfile("Kristian", "number")
Can you walk through the steps your code will take?
Nope. When i
is 0, it either returns “No such contact”, “No such property”, or a match.
OK cool, looks like i understood. My code only undergoes looping once, cause it returns the value in the first loop,leaving the other objects unchecked. Right?
1 Like
Exactly! Now you have to figure out how to only return “No such contact” after every value has been checked.
yeah, i got it. thanks alot