Tell us what’s happening:
This is not to ask for help but to report a bizarre testing error. As you can see I had to separate out the objects into for different arrays in order for all the tests to pass - otherwise, the for loop was unable to read the properties from the second object and moving on. Has anyone gone through this? I was not sure where to report this bug.
Your code so far
//Setup
var contacts = [
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["JavaScript", "Gaming", "Foxes"]
},
];
var contacts2 = [
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
]
var contacts3 = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
]
var contacts4 = [
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
]
function lookUpProfile(name, prop){
// Only change code below this line
// for (var i = 0; i < contacts.length; i++) {
// if (name === contacts[i]['firstName']) {
// if (contacts[i].hasOwnProperty(prop)) {
// return contacts[i][prop];
// } else {
// return 'No such property';
// }
// } else {
// return 'No such contact';
// }
// }
if (name === contacts[0]['firstName']) {
if (contacts[0].hasOwnProperty(prop)) {
return contacts[0][prop];
} else {
return 'No such property';
}
} else if (name === contacts2[0]['firstName']) {
if (contacts2[0].hasOwnProperty(prop)) {
return contacts2[0][prop];
} else {
return 'No such property';
}
} else if (name === contacts3[0]['firstName']) {
if (contacts3[0].hasOwnProperty(prop)) {
return contacts3[0][prop];
} else {
return 'No such property';
}
} else if (name === contacts4[0]['firstName']) {
if (contacts4[0].hasOwnProperty(prop)) {
return contacts4[0][prop];
} else {
return 'No such property';
}
} else {
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 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup