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 Overflowtrying to understand this profile lookup in JavaScript - Stack Overflow
Profile lookup on freecode camp checkpoint [closed]
"Profile Lookup" — walkthrough?
freeCodeCamp Challenge Guide: 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.
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"))
Can anyone give me a hint as to what I might be doing wrong? Currently getting 2/5 tests correct.
function lookUp(firstName, prop){
// Only change code below this line
var result;
for(var i = 0;i<contacts.length;i++)
{
if((contacts[i].firstName===firstName) && (contacts[i].hasOwnProperty(prop)))
{
result = (contacts[i][prop]);
}
else if (contacts[i].firstName!==firstName) {
result = "No such contact";
}
else if (contacts[i].hasOwnProperty(prop) !== true) {
result = "No such property";
}
}
return result;
}
Taken from Challenges: Profile Lookup.
Here's what I had, its basically a variation of what Minimanimalist did but without the flag. Mine works on the assumption that if the contact exists and it has the property we are looking for it will be returned before it gets to the "No such contact". Please excuse formatting.
function lookUp(firstName, prop){// Only change code below this linefor(var i=0;i<contacts.length;i++){ if(contacts[i].firstName === firstName){ if(contacts[i].hasOwnProperty(prop)){ return contacts[i][prop];} else { return "No such property";} }}return "No such contact";
Looks like you're returning too quickly. You want to be able to check all the contacts first.