Hello All,
I solved the profile lookup challenge in the JavaScript section but I don’t understand my solution fully. Specifically, why does my code work only when I include the break statements? Any help would be greatly appreciated. Below is 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(firstName, prop){
// Only change code below this line
var result = “”;
var i = 0;
while (i < contacts.length + 1) {
if (i == contacts.length) {
result = "No such contact";
}
else {
if (i < contacts.length) {
if (contacts[i].firstName === firstName) {
if (contacts[i].hasOwnProperty(prop)) {
result = contacts[i][prop];
break;
} else {
result = "No such property";
break;
}
}
}
}
i++;
}
return result;
// Only change code above this line
}
// Change these values to test your function
lookUpProfile(“Harry”, “number”);
Thank you very much fellow campers!