Tell us what’s happening:
Hello, I’m trying to solve this coding challenge and I don’t understand why this isn’t working. Everything in my code is identical to the official solution, but I chose to use a forEach loop instead of a regular for loop. Can anyone explain what I’m doing wrong here? Would be much appreciated
To be more specific, the following code only returns: ‘No such contact’…
I thought maybe it’s not reading cur[prop] so I logged it to the console and it works just fine. So I should be able to return cur[prop] right?
Official Solution:
https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup/
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup
My Solution:
//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
contacts.forEach(cur => {
if (cur.firstName === name) {
if (cur.hasOwnProperty(prop)) {
return cur[prop];
}
return 'No such property';
}
})
return 'No such contact';
// Only change code above this line
}
// Change these values to test your function
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/77.0.3865.90 Safari/537.36
.