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 Overflow
🌐
freeCodeCamp
forum.freecodecamp.org › guide
freeCodeCamp Challenge Guide: Profile Lookup - Guide - The freeCodeCamp Forum
June 2, 2023 - Profile Lookup Problem Explanation Change the code below // Only change code below this line and up to // Only change code above this line. Ensure that you are editing the inside of the lookUpProfile() function. This function includes two ...
🌐
CodePen
codepen.io › secan › pen › oLXYvq
FreeCodeCamp : Basic Javascript : Profile Lookup
//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(firstName, prop){ // Only change code below this line var contact; for (var i=0; i<conta
Discussions

trying to understand this profile lookup in JavaScript - Stack Overflow
Hello guys I am having some issues understanding this challenge from FreeCodeCamp More on stackoverflow.com
🌐 stackoverflow.com
Profile lookup on freecode camp checkpoint [closed]
So I'm going through freecodecamp and I'm solving the problems there, to keep in the loop with the programming and I've stumbled on a snag, and I'm not quite sure what's wrong. So I have an array of More on stackoverflow.com
🌐 stackoverflow.com
"Profile Lookup" — walkthrough?
Link to challenge: https://www.freecodecamp.org/challenges/profile-lookup I spent a few hours on this but a solution wasn’t clicking. I knew that I had to write an if/else statement but wasn’t sure where to go from there. Could you walk me through how to solve it to help me understand the ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
September 2, 2017
freeCodeCamp Challenge Guide: Profile Lookup
Profile Lookup We have an array of objects representing different people in our contacts lists. A lookUpProfile function that takes name and a property (prop) as arguments has been pre-written for you. The function should check if name is an actual contact’s firstName and the given property ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
1
November 5, 2022
🌐
GitHub
github.com › EQuimper › CodeChallenge › blob › master › javascript › FreeCodeCamps › Basic JavaScript › Profile Lookup.md
CodeChallenge/javascript/FreeCodeCamps/Basic JavaScript/Profile Lookup.md at master · EQuimper/CodeChallenge
//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(firstName, prop){ var msg = 'No such contact'; for (var p in contacts) { if (contacts[p].firstName === firstName && contacts[p].hasOwnProperty(prop)) { msg = contacts[p][prop]; } else if (!contacts[p].hasOwnProperty(prop)) { msg = 'No such property'; } } return msg; // Only change code above this line } // Change these values to test your function lookUpProfile("Harry", "likes");
Author   EQuimper
🌐
Stack Overflow
stackoverflow.com › questions › 65312020 › trying-to-understand-this-profile-lookup-in-javascript
trying to understand this profile lookup in JavaScript - Stack Overflow
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup · And here is my solution ·
🌐
Gitter
gitter.im › FreeCodeCamp › Help
FreeCodeCamp/Help - Gitter
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(firstName, prop){ for(var i=0;i < contacts.length;i++){ if (contacts[i].hasOwnProperty(prop)) { if(contacts[i].firstName === firstName){ return contacts[i][prop]; } else { return "No such contact"; } } // Only change code above this line } return "No such property"; } // Change these values to test your function lookUpProfile("Harry", "likes");
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
"Profile Lookup" — walkthrough? - JavaScript
September 2, 2017 - Link to challenge: https://www.freecodecamp.org/challenges/profile-lookup I spent a few hours on this but a solution wasn’t clicking. I knew that I had to write an if/else statement but wasn’t sure where to go from ther…
Find elsewhere
🌐
GitHub
github.com › Rafase282 › My-FreeCodeCamp-Code › wiki › Lesson-Review-Profile-Lookup
Lesson Review Profile Lookup · Rafase282/My-FreeCodeCamp-Code Wiki · GitHub
January 6, 2017 - Solution ahead! function lookUp(firstName, prop) { // Only change code below this line var answer = "No such contact"; contacts.some(function(arg) { if (arg.firstName === firstName && arg.hasOwnProperty(prop) === true) { answer = arg[prop]; } else if (arg.hasOwnProperty(prop) === false) { answer = "No such property"; } }); return answer; // Only change code above this line } // Change these values to test your function lookUp("Kristian", "lastName");
Author   Rafase282
🌐
YouTube
youtube.com › we will code
Profile Lookup, freeCodeCamp Basic Javascript - YouTube
Show your Support, Buy a Sticker! https://believerationally.com/shopIn this challenge, we learn how to look through an array of contacts in javascript and s
Published   March 27, 2017
Views   14K
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
freeCodeCamp Challenge Guide: Profile Lookup - JavaScript
November 5, 2022 - A lookUpProfile function that takes name and a property (prop) as arguments has been pre-written for you. The function should check if name is an actual contact’s firstName and the given property ...
🌐
Reddit
reddit.com › r/freecodecamp › javascript profile lookup
r/FreeCodeCamp on Reddit: Javascript Profile Lookup
December 25, 2021 -

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"))

🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Basic Java Script - Profile Lookup Solution - JavaScript
July 17, 2020 - Hey guys, I’ve got a little problem: i’ve written down a correct solution (when I’m checking it by copying the code to codepen.io and calling the function via console with the given testcases - everythings fine). My code so far: // Setup var contacts = [ { "firstName": "Akira", "lastName": "Laine", "number": "0543236543", "likes": ["Pizza", "Coding", "Brownie Points"] }, { "firstName": "Harry", "lastName": "Potter", ...
🌐
Stack Overflow
stackoverflow.com › questions › 76625236 › freecodecamp-challenge-basic-javascript-profile-lookup-solution
properties - freeCodeCamp Challenge - Basic Javascript - Profile Lookup Solution - Stack Overflow
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[i].hasOwnProperty(prop)) { return contacts[i][prop] } else { return "No such property" } } } return "No such contact" // Only change code above this line }
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Basic JavaScript - Profile Lookup, *help understanding solution* - JavaScript - The freeCodeCamp Forum
May 29, 2023 - Tell us what’s happening: There’s one thing in this code that I don’t understand. In the function “lookUpProfile” I see that if I pass (“Kristian”, “Likes”) it will return Kristian’s likes. However, it still says “retur…
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Build a Profile Lookup - Build a Profile Lookup
3 weeks ago - Tell us what’s happening: why if statement is not working even when the values in function are correct. Your code so far let contacts = [ { firstName: "Akira", lastName: "Laine", number: "0543236543", …
🌐
Blogger
questionsans.blogspot.com › 2017 › 05 › freecodecamp-profile-lookup-sample.html
Questions and Answers: FreeCodeCamp Profile Lookup Sample Solution
May 11, 2017 - //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(firstName, prop){ // Only change code below this line var returnValue = ""; for(var i=0;
Top answer
1 of 2
4

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:

  • false if no Contact with the requested firstName exists
  • undefined if a Contact matches but the requested property doesn't exists
  • string|array if 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.

2 of 2
-1
>

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"));

🌐
Medium
medium.com › @kyokyox2 › note-basic-javascript-profile-lookup-951cbd3286c7
[note] Basic JavaScript: Profile Lookup/ freecodecamp | by K | Medium
May 2, 2019 - 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){}