Hello, I’m trying to loop through an array that contain objects as elements but looking at the error message I got, it appears my loop stops after its first iteration. That is when the lookUpProfile() function is called using arguments that matches the first item of the array, the code works. Below are my actual code and the error message respectively. Code:
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++) {
var particularContact = contacts[i];
//console.log(particularContact);
if(particularContact["firstName"] == name && particularContact.hasOwnProperty(prop)) {
//console.log(particularContact[prop]);
return particularContact[prop];
}
if(particularContact["firstName"] != name) {
return "No such contact";
}
if(!(particularContact.hasOwnProperty(prop))){
return "No such property";
}
}
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
**Error Message:**
// running tests
"Kristian", "lastName" should return "Vos"
"Sherlock", "likes" should return ["Intriguing Cases", "Violin"]
"Harry","likes" should return an array
// tests completed
please, when you ask for help, add the link of the challenge and format your code with the backticks (using the “Preformatted text” in the formatting tools works too
If you use the “Ask for help” button in the challenge page, that is done automatically
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 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.
You can have a case in the loop where name is found but the prop isn’t, from this case you can return. If the loop has finished and you haven’t hit the 2 returns (name and prop found, name found-prop not found), then it means that you can return that no name was found (i.e. return No such contact).
Thanks. Your suggestion actually worked. However, I'm still curious about why that particular line, i.e the return **"No such contact"**, has to be placed beyond the loop.
if(particularContact["firstName"] != name) {
return "No such contact";
}
A return statement stop the loop from running, if you have Akira, awesome, it works, but if you have an other name when you check if Akira and the name are different, and they are different, the code return 'no such contact' is executed, and the loop breaks and the function returns something and stop executing.
If you put that line outside of the loop it means that first each contact is evaluated to see if they have name as firstName and if everyone doesn’t have it and so no return statement is executed inside the loop, your loop ends without returning anything, and after the loop there is the “no such contact” thing that returns that the loop didn’t find a corresponding contact in the list