Ok I'm dumb, I exited my for loop too early:
function lookUp( firstName, prop ){
for( var i = 0; i < contacts.length; i++ ){
if( firstName == contacts[i].firstName ) {
if( contacts[i].hasOwnProperty( prop ) ) {
return contacts[i][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
}
This works.
Answer from dingo_d on Stack OverflowProfile lookup on freecode camp checkpoint [closed]
Javascript Profile Lookup
Where are the contacts object
More on reddit.comBuild a Profile Lookup - Build a Profile Lookup
Basic JavaScript - Profile Lookup
Videos
Ok I'm dumb, I exited my for loop too early:
function lookUp( firstName, prop ){
for( var i = 0; i < contacts.length; i++ ){
if( firstName == contacts[i].firstName ) {
if( contacts[i].hasOwnProperty( prop ) ) {
return contacts[i][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
}
This works.
I think you are confused about what return does. Your code will only do one iteration and return "No such contact". Return immediately stops the function to execute. Here is a fiddle of what I mean demonstrated with console.log https://jsfiddle.net/oegw3a4y/
In your situation, the first iteration evaluates to false in the first if statement and immediately goes to the else.
Hi guys can you tell me what might be wrong with this code? Looked at it for 30 minutes but cant figure out what is wrong here.
function lookUpProfile(name, prop) {
// Only change code below this line
for (let i = 0; i < contacts.length; i++){
if (contacts[i].firstName === name){
if (contacts.hasOwnProperty(prop)) {
return contacts[i][prop];
} else {
return 'No such property';
}
}
} return "No such contact"
// Only change code above this line
}
console.log(lookUpProfile("Kristian", "lastName"))
Ok I'm dumb, I exited my for loop too early:
function lookUp( firstName, prop ){
for( var i = 0; i < contacts.length; i++ ){
if( firstName == contacts[i].firstName ) {
if( contacts[i].hasOwnProperty( prop ) ) {
return contacts[i][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
}
This works.
I think you are confused about what return does. Your code will only do one iteration and return "No such contact". Return immediately stops the function to execute. Here is a fiddle of what I mean demonstrated with console.log https://jsfiddle.net/oegw3a4y/
In your situation, the first iteration evaluates to false in the first if statement and immediately goes to the else.
Hi everyone, I'm new in programming, and I want to ask you if you could tell me why my code doesn't work? I have seen other solutions, and are ok for me, but this code that I made seems ok for me too, but I don't know why it doesn't work.
If I don't write the lasts “else if” and the “else return” the 3 first tests past, but when I try to accomplish the last “No such contact” and “No such property” instructions then the last 3 tests past but not the 3 first test. What am I missing? Thank you for your time.
Here is the link: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup