Tell us what’s happening:
I did the Basic Javascript: Profile Lookup profile and has passed it but was wondering why this code I have pasted down below isn’t also a valid solution?
Here is the instructions for this lesson:
We have an array of objects representing different people in our contacts lists.
A lookUpProfile
function that takes name
and a property ( prop
) as arguments has been pre-written for you.
The function should check if name
is an actual contact’s firstName
and the given property ( prop
) is a property of that contact.
If both are true, then return the “value” of that property.
If name
does not correspond to any contacts then return "No such contact"
.
If prop
does not correspond to any valid properties of a contact found to match name
then return "No such property"
.
I did struggle on this problem for a bit, and had to use the hints I posted the hints down below. However hint 3 confuses me and is what had me stumped. Why does my code not work if I don’t follow hint 3 instructions.
It doesn’t explain why either can someone please tell me why?
Hint 1
Use a for
loop to cycle through the contacts list.
Hint 2
Use a nested if
statement to first check if the firstName matches, and then checks if
the prop matches.
Hint 3
Leave your return "No such contact"
out of the for
loop as a final catch-all.
freeCodeCamp.org](https://www.freecodecamp.org/learn)
Here is the wrong code that I was trying to write for the problem
// 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"]
}
];
//console.log(contacts[0]['firstName'])
function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (name === contacts[i]['firstName'] && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else if (name === contacts[i]['firstName'] && !contacts[i].hasOwnProperty(prop)){
return "No such property";
} else {
return "No such contact";
}
}
// Only change code above this line
}
console.log(lookUpProfile("Kristian", "lastName"));
The correct code I wrote…
// 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"]
}
];
//console.log(contacts[0]['firstName'])
function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (name === contacts[i]['firstName'] && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else (name === contacts[i]['firstName'] && !contacts[i].hasOwnProperty(prop)) {
return "No such property";
}
}
return "No such contact"
// Only change code above this line
}
console.log(lookUpProfile("Kristian", "lastName"));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36
.
Challenge: Profile Lookup
Link to the challenge: