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.
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.
You have very repetitive code. This can be optimized. I'm not sure whether your function name lookUpProfile is the correct one, as you actually look up a property of a Contact and not its whole profile. But if the task is to look up a certain property of a Contact you can simplify the code to this:
function lookUpProfile(firstName, prop) {
for (let contact of contacts) {
if (contact.firstName === firstName) {
return contact[prop];
}
}
return false;
}
How it works
It loops through all contacts:
for (let contact of contacts) {
It tests whether the firstName matches:
if (contact.firstName === firstName) {
In case a match is found it returns the requested property or undefined:
return contact[prop];
In case no match is found it returns false:
return false;
What it returns
This function will have a mixed return value. The code that is calling the function can now deal with the result. It will return one of the following:
falseif noContactwith the requestedfirstNameexistsundefinedif aContactmatches but the requestedpropertydoesn't existsstring|arrayif a match was found and the property exists
So all those calls will work:
console.log(
lookUpProfile(),
lookUpProfile('A'),
lookUpProfile('Sherlock'),
lookUpProfile('Sherlock', 'number'),
lookUpProfile('Kristian', 'likes')
);
The return values are:
false false undefined 0487345643 Array [ "Javascript", "Gaming", "Foxes" ]
What the code doesn't do:
It doesn't check whether there's a choice for Contacts, i.e. if more than one Contact with that name exists. It will always exit at the first match.
jsFiddle Demo
Try before buy
Update "No such property"
Regarding the OP's comment about a different return value, I would suggest keeping the code as is and let other parts worry about a concrete message:
var value = lookUpProfile('Sherlock', 'random');
if ('undefined' === typeof value) {
console.log('No such property');
};
If your task is to include that text into the function you can replace this line:
return contact[prop];
with:
return 'undefined' === typeof contact[prop] ? 'No such property' : contact[prop];
This is a lot harder to handle, as the string "No such property" could be an actual value of a property.
We are looking for answers that provide insightful observations about the code in the question. Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed.
// Setup
const 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++ ) {
if (contacts[i]['firstName'] === name && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
if (contacts[i]['firstName'] === name && !(contacts[i].hasOwnProperty(prop))) {
return 'No such property';
}
}
return 'No such contact';
// Only change code above this line
}
console.log(lookUpProfile("Kristian", "lastName"));
console.log(lookUpProfile("Sherlock", "likes"));
console.log(lookUpProfile("Harry", "likes"));
console.log(lookUpProfile("Bob", "number"));
console.log(lookUpProfile("Bob", "potato"));
console.log(lookUpProfile("Akira", "address"));
console.log(lookUpProfile("Akira", "likes"));