Profile Lookup MTayachi

Tell us what’s happening:
I’m stuck, could use some help please, I don’t know what’s wrong in the code, thank you.

Your code so far

js

//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(name, prop){
// Only change code below this line
for (var i=0; i<contacts.length; i++) {
  if (name == contacts[i].firstName && contacts[i].hasOwnProperty(prop) == true) {
    return contacts[i].prop;
  } else if (name == contacts[i].firstName && contacts[i].hasOwnProperty(prop)== false){
    return "No such property";
  } else {
    return "No such contact";
  }
}
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Mike", "lastName");
console.log(lookUpProfile("Sherlock", "lastName"))

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

There are three things:
Firstly, it is better to use the strict equality operator === in your comparisons.
Secondly, move your last return statement return "No such contact" outside of the for loop.
Thirdly, your return statement return contacts[i][prop] should be accessed using the array notation as in some cases an array is returned.

2 Likes

There are three return options: return the property, return No such property, or return No such contact. Currently, you are handling all three of those on the first record. By having the No such contact inside the loop, as @sabahat70 says, you’re breaking the for loop entirely.

And, as was pointed out, there are two different ways of accessing properties:

  • if you are accessing them directly, you can use ‘dot notation’: collection[i].lastName is perfectly fine, as lastName is an actual property on the object.
  • if you are accessing them by a variable (like prop, which is a string passed as a parameter), you need to use ‘bracket notation’: collection[i][prop] will see this as collection[0]["lastName"], and will work properly for what you’re doing.
1 Like

It’s worth expanding on why the return statement goes outside of the for statement.

When it does its first for loop and sees the else statement, it returns data to the function, ending the loop. You need it to loop through all of the data first and if it can’t find it, then you return the fallback text.

2 Likes

It worked, thank you for your help. I’m still however a lil lost on why it should be outside the loop, I mean it is inside it, but under a condition, why the “for” loop keeps going on to the “else” statement if one of the two first conditions is fufilled, and therefore overriding the previous “return”? If I understood correctly, a function stops executing commands once it reaches a “return” statement, and the “if” conditioning doesn’t need a “break;” like the “switch”. Thank you.

So here’s what the code should be doing, as “pseudocode”:

  • Loop over all the contacts
  • Does this contact’s firstName match the one I was given?
    • If so, do we have the given property on this contact?
      • if NOT, we need to return ‘No such property’ (note, I’m nested here!)
      • if SO, we can return the property!
    • If we get here, then this is "the contact’s firstName didn’t match. Keep looping, let’s check the next one!
  • Until we run out of contacts. When that happens (after the for loop), then we need to say “No such contact”.
1 Like

there is no overriding a return statement, if it is reached, it is executed, the value is returned from the function and the function stops

function is searching for contact “Sherlock” (name is "Sherlock")
first iteration of the loop, i is 0, firstName is "Akira"
if statement, condition is false, it is not executed
else if statement, condition is false, it is not executed
else statement, executed as everything else is false, return statement executed, the function STOPS, as such it is impossible to meet other return statements in following iterations of the loop

2 Likes