Tell us what’s happening:
Hey guys I have long time on this and I am just not getting it, I do not know why my code is not working. Any help help would be really helpful.
Your code so far
// 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
for ( var i = 0; i < contacts.length; i++)
{
if ( contacts[i].firstName.hasOwnProperty(name) && contacts[i].hasOwnProperty(prop)) {
return "value"
} else if ( contacts[i].firstName != name){
return "No such contact" ;
} else if ( contacts[i] != prop ){
return "No such property"
}
}
// Only change code above this line
}
lookUpProfile("Akira", "likes");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0.
When any return statement is executed, the function is immediately exited, even if the for loop has not completed. You are returning a value in the first iteration of the for loop, so there are no further iterations.
You will to rethink your overall algorithm for this challenge. Think about when you can for sure return “No such property” vs. “No such contact”. Make sure to walk through your algorithm with each test case before writing any code.
function lookUpProfile(name, prop) {
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName.hasOwnProperty(name) && contacts[i].hasOwnProperty(prop)) {
return "value"; // Do you want to return the string "value" here?
} else if (contacts[i].firstName != name) {
return "No such contact"; // Have you checked every single name at thsi point?
} else if (contacts[i] != prop ) {
return "No such property" ;
}
}
// Only change code above this line
}
I got all the way up to here, but I am getting a syntax error on the else part
function lookUpProfile(name, prop){
// Only change code below this line
for ( var i = 0; i < contacts.length; i++) {
if ( contacts[i].firstName.hasOwnProperty(name) {
if (contacts[i].hasOwnProperty(prop){
return contacts[i][prop];
} else { // there is an error on this line that i don't understand
return "No such property"
}
}
}
return "No such contact"
// Only change code above this line
}
function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName.hasOwnProperty(name) {
if (contacts[i].hasOwnProperty(prop) { // Your error is actually up here, check your ()s
return contacts[i][prop];
} else {
return "No such property"
}
}
}
return "No such contact"
// Only change code above this line
}
thats right I forgot the parentheses, I will leave this until here for now and I will get back tomorrow if can not resolve this.
function lookUpProfile(name, prop){
// Only change code below this line
for ( var i = 0; i < contacts.length; i++) {
if ( contacts[i].firstName.hasOwnProperty(name)){
if (contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
} else {
return "No such property"
}
}
}
return "No such contact"
// Only change code above this line
}
I am doing what is below
The function should check if name is an actual contact’s firstName and the given property ( prop ) is a property of that contact.
thank you I changed it to this if ( contacts[i].firstName == name)t and it worked but I dont understand why it din’t work when I use the .hasOwnProperty
object.hasOwnProperty(property) just checks if the object on the left of the dot has a property accessed via the value stored in the property variable.
let myObject = {
dog: "woof",
cat: "meow",
}
// Try with a defined property
let checkProp = "dog";
console.log(myObject.hasOwnProperty(checkProp)); // true
// Try with a value
checkProp = "meow";
console.log(myObject.cat.hasOwnProperty(checkProp)); // false