Tell us what’s happening:
I have been trying to get this to work but for some reason it always returns undefined any hints?
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
if (name == contacts[3].firstName || contacts[2].firstName || contacts[1].firstName || contacts[0].firstname) {
if (name == contacts[3].firstName) {
return(contacts[3].prop)
}
if (name == contacts[2].firstName) {
return(contacts[2].prop)
}
if (name == contacts[1].firstName) {
return(contacts[1].prop)
}
if (name == contacts[0].firstName) {
return(contacts[0].prop)
}
else {
return ("No such contact")
}
}
// Only change code above this line
}
console.log(lookUpProfile("Kristian", "lastName"))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36
.
Challenge: Profile Lookup
Link to the challenge:
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
This line will cause some bugs for you. The first conditional is correct, but the rest are not. contacts[2].firstName
, for example, will evaluate to true
(unless undefined). So your if
block will execute, but if the nested if
statements all evaluate to false, your function will not hit a return
statement and will then implicitly return undefined
.
ILM
November 13, 2020, 8:12pm
3
NoneCoder:
contacts[2].prop
also these are always undefined as you can’t use variables with dot notation and the objects don’t have properties named prop
Good catch! I didn’t even notice that
ohh thanks i did not think of that
// 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
if (name == contacts[3].firstName || name == contacts[2].firstName || name == contacts[1].firstName || name == contacts[0].firstname) {
if (name == contacts[3].firstName) {
return(contacts[3][prop])
}
if (name == contacts[2].firstName) {
return(contacts[2][prop])
}
if (name == contacts[1].firstName) {
return(contacts[1][prop])
}
if (name == contacts[0].firstName) {
return(contacts[0][prop])
}
else {
return ("No such contact")
}
}
// Only change code above this line
}
console.log(lookUpProfile("Sherlock", "likes"))
i almost have it
ILM
November 13, 2020, 8:45pm
9
once the name check, you always return contacts[...][prop]
what happens if the value of prop
is not a property of the object?
i’m just gonna watch the video i don’t think im smart enough
You can do this! You’re very much on the right track.