Why the if statement not working despite the two conditions are met?
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++){
if(prop === contacts[i].hasOwnProperty(prop) && name === contacts[i][prop]){
return contacts[i][prop];
}else {
return "Not working";
}
}
// Only change code above this line
}
lookUpProfile("Harry", "firstName");
You exit in the first loop with either the name or with "Not working"
.
To prevent this, move the return value "Not working"
outside of the loop, at then of all looping.
A direct comparison of the value with the property is enough. You need to return the object, instead of just the known name.
function lookUpProfile(name, prop) {
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (name === contacts[i][prop]) {
return contacts[i];
}
}
return "Not working";
// Only change code above this line
}
lookUpProfile("Harry", "firstName");
Do this instead and fix the if contacts[i].hasOwnProperty(prop) is a boolean.
function lookUpProfile(name, prop){
for(var i =0; i < contacts.length; i++){
if(contacts[i].hasOwnProperty(prop) && name === contacts[i][prop]){
return contacts[i][prop];
}
}
return "Not working";
}
console.log(lookUpProfile("Harry", "firstName"));
The contacts[i].hasOwnProperty(prop)
only returns boolean values (True or False). Therefore when you call prop === contacts[i].hasOwnProperty(prop)
it will always return false, since "firstName !== true"
.
So you might fix it with if(contacts[i].hasOwnProperty(prop) && name === contacts[i][prop]){...}
There are two errors, the first is the wrong use of the hasOwnProperty
which returns a bool value (true or false). More information .
The other error is that the loop returns after the first entry, to prevent this put the return "Not working"
line after the loop finished.
I've made a working example:
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++) { if (contacts[i].hasOwnProperty(prop) && name === contacts[i][prop]) { return contacts[i][prop]; } } return "Not working"; // Only change code above this line } let res = lookUpProfile("Harry", "firstName"); console.log(res);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.