Basic JavaScript - Profile Lookup

Tell us what’s happening:
Describe your issue in detail here.
Not really sure what’s wrong in here. I’ve spent the last hour trying to figure it out.
Your code so far

// 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){
            if (contacts[i].hasOwnProperty(prop)){
                return contacts[i][prop];
              } else {
                return "No such property";}
          } else {
            return "No such contact";}
    }
    // Only change code above this line
  }

lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

Return statements immediately shop the function.

Note - I would format your code

What you mean stop the function? Isnt that ok if “IF” statement is not met?

If the if condition is not met, then the else automatically executes.

In this case, the else has a return statement.

Ok thats clear, but Im getting this error

lookUpProfile("Kristian", "lastName")

should return the string

Vos
lookUpProfile("Sherlock", "likes")

should return

["Intriguing Cases", "Violin"]
lookUpProfile("Harry", "likes")

should return an array

What is your updated code?

1 Like

I got the problem, is where the last return is placed. I dont get why it has to be placed outside the FOR loop:
(it should return if the first IF statement is exited from my understanding…)

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

Basically, by using the return keyword you do not let the loop to continue its process after the firs iteration.

The following works because the loop could iterate once and path through a condition, but since you are returning a value in any cases ( if the one conditions pass ) it will exit the loop process.

Reminder:

  • a for loop can be exited if one use the keyword break or return after statements.
  • a function is exited as soon as it execute any first return it program mets.
lookUpProfile("Akira", "Likes") // likes: ["Pizza", "Coding", "Brownie Points"]

lookUpProfile("Kristian", "lastName") // No such contact

The above does not work for the same reasons above.
The first iteration over your array will be to evaluate Akira:

  • since Kristian is not equal to Akira ( first level condition ) : it will go through the else statement and return “No such contact” and exit the function along the way, not letting the loop continuing its process

How to do:
:small_orange_diamond: What you will want to do is to have a variable outside the loop ( variables are reassignable if you use var or let keywords )

:small_blue_diamond: Why outside the loop ?
Because if you would do inside, your variable initialisation / creation would be initialised and/or reassigned after each iteration.

Setting it outside would give you the flexibility to assign only when it is needed

:small_orange_diamond: Then, within your conditions, you will not want to return a value but want to update the variable to assign the right value when needed and providing your loop to continue if the first item ( and so on ) does not met the conditions until it encounter the case that is meeting the condition → this is at this moment you want to assign the variable

:small_orange_diamond: Then after the loop block, once the loop finished looping, you could have some value stored in the variable you defined earilier: this is where you will want to return the final value if there is something assigned to your variable or you would returning something else to handle the case where nothing was found.

:slight_smile: Hope this could help