Why do I have to return something after the for loop? & don't i have to check .hasOwnProperty== true?

Hi all -
I’m having a hard time understanding a couple of things on this exercise.
First issue: why do I have to return something after the for loop? I included my last return “No such contact” (it’s commented out) within the for loop–which made sense to me-- but it wouldn’t work unless I place it outside the loop. I’m going crazy here. Can someone explain it in plain english please?
Second issue: I understand using .hasOwnProperty returns a boolean, which is why I made it to check it is was true in order for the code to execute. However, I’ve seen the answer for this exercise and i noticed it didn’t include “== true” or “==false” why?

Thanks in advance for any and all guidance!

  **Your code so far**

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

lookUpProfile("Kristian", "lastName");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

The very first time your function executes a line with a return statement, the entire function halts. Inside of your loop, this means that the very first time you find a contact that does not have a first name that matches, your function runs the line

        return "No such contact";

and stops without checking any more contacts.


As far as your question with hasOwnProperty, adding == true is redundant.

myObject.hasOwnProperty(someProperty) 

will either return true or false. The comparison

someValue == true

returns either a true or false value. Why not just use the fact that hasOwnProperty() returns a boolean rather than calculating a boolean value from the boolean value it returns?

1 Like

Thank you so much for this explanation. Now i can get why return should be placed last.
As for the .hasOwnProperty(), i thought that for this exercise the value needed to return true in order for the loop to work. I thought that if it returned false, the code was not going to execute.

P.S. Sorry for the delayed response!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.