Tell us what’s happening:
I can’t figure out the logic. I know contacts[i].firstName gives the correct values, but I cannot get values for name or prop within the function when I console.log them. They should produce “Arika” and “likes” When I run the function in Visual Studio Code I always get “No such contact” so I think the mistake is in the first if statement, but I can’t figure it out. I also don’t know if I’m using undefined properly. The values are declared but when there’s no value associated with them I think that generates an undefined, so I think they’re okay. If not, please let me know and why. Thanks!
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 (let i = 0; i < contacts.length; i++) { // The function should check if name is an actual contact's firstName and the given property (prop) is a property of that contact.
if ((name===contacts[i].firstName) && (prop===contacts[i].firstName||prop===contacts[i].lastName||prop===contacts[i].number||prop===contacts[i].likes)) {
return contacts[i].firstName; // If both are true, then return the "value" of that property.
} else if (contacts[i].name == undefined) {
return "No such contact"; // If name does not correspond to any contacts then return "No such contact"
} else if (contacts[i].prop ==undefined) {
return "No such property";
} // If prop does not correspond to any valid properties of a contact found to match name then return "No such property"
// Only change code above this line
}
}
// Change these values to test your function
console.log(lookUpProfile("Akira", "likes"));
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
I’m not following your hint. Arrays should be called like this: contacts[i] and the property should be called: contacts[i].firstName. The “likes” property has a space in the first object, but when I replace my code with contacts[i][likes] it threw an error: likes not defined. In fact, I changed every object to contacts[i][property] and they all threw similar errors. How else would you write this? I went back and reviewed, but I didn’t see anything except a call from one parent array to an array within an object, and there are none like that in this problem. Thanks.
ok now here is some more hint i think now you solve
var myNumber ="";
var checkName="";
for (var i=0;i<contacts.length;i++){
if (contacts[i].firstname===name){ ///try this
checkName=true;
myNamber = i;}
}
//Pseudo code
if checkName is not true {
return "no such contact ";}
if checkName is true {
var property = hasOwnProperty ;} //check it
if checkName is true and property is true {
return contact’s property;}
else if (property is not true){
return “no such property”;}
It’s still not working. I went to YouTube and found a solution, but although it worked for the presenter, it didn’t work for me. I keep getting “No such contact.” Here’s the 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(name, prop) {
// Only change code below this line
for (let i = 0; i < contacts.length; i++) { // The function should check if name is an actual contact's firstName and the given property (prop) is a property of that contact.
if (contacts[i].firstname === name) {
if (contacts[i].hasOwnProperty(prop)) { // If both are true, then return the "value" of that property.
return contacts[i].prop;
} else {
return "No such property";
}
}
}
return "No such contact";
}
// Change these values to test your function
console.log(lookUpProfile("Akira", "likes"));
function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++){
if(contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}
else{
return “No such property”;
}
}
}
return “No such contact”;
// Only change code above this line
}
// Change these values to test your function
lookUpProfile(“Harry”, “likes”);
function lookUpProfile(name, prop){
// Only change code below this line
for (var x = 0; x < contacts.length; x++){
if (contacts[x].firstName === name) {
if (contacts[x].hasOwnProperty(prop)) {
return contacts[x][prop];
} else {
return "No such property";
}
}
}
for (var x = 0; x < contacts.length; x++){
if(contacts[x].firstName !== name){
return "No such contact";
}
}
}
If you need some help with this you may want to open a thread just for you using the Ask for help button
It would be easier to help you that way
Do you really need the second loop? Think of what is happening in your code - the first loop is checking if there is an object with a certain name, and if there is returning something. But if there is not an object with that name the loop ends - and now do you really need he second loop?
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.