Profile Lookup, Alternate Solution Confusion

Tell us what’s happening:
I’ve tried 2 different solutions:

Solution 1.

for (var x = 0; x < contacts.length; x++){
     if (contacts[x].firstName === name) {
       if (contacts[x][prop]) {
            return contacts[x][prop];  } 
       else {   return "No such property";   }
          }
      }
return "No such contact";

Solution 2.
(The one given in guide)

for (var x = 0; x < contacts.length; x++){
   if (contacts[x].firstName === name) {
       if (contacts[x].hasOwnProperty(prop)) {
           return contacts[x][prop];
       } else {
           return "No such property";
       }
   }
}
return "No such contact";

They both work. The only difference is the second if statement:

  1. if (contacts[x][prop])
    2.if (contacts[x].hasOwnProperty(prop))

Why does the if statement for Solution 1 work. Does it return a Boolean instead of the property?

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

First of all, make sure you are formatting your code in the editor. It makes it really difficult to read without it.

They both return booleans. That’s why statements inside curly brackets get executed. Often you will get the same result with or without hasOwnProperty. The latter ignores properties that are inherited.

You could read more here.

I tried formatting it correctly it change after submitting it for some reason. I am going to take a look at the article. Thanks!

After reading the above article. I am still confused why contacts[x][prop] returns a Boolean in an if statement and a value elsewhere (such as the following return statement).

It is not that the above returns a Boolean. It is the fact that the if statement condition gets evaluated as a Boolean. If contacts[x][prop] evaluates to a “Truthy” value, then it is the same as testing for a “true” condition. A “Truthy” value is any value which is NOT NaN, “”, undefined, false, 0, or null. These values are considered “Falsy”.

When you reference an object property which does not exist in an object, it evaluates to undefined, which would make the if statement false. However, you do have to be careful in using this syntax, because if the property existed and had a “Falsy” value, you would incorrectly force the if statement to evaluate to false.

For example, let’s say I have the following object:

var gamer = {
  name: 'Randell',
  numYearsPlaying: 0,
  system: 'XBox'
};

An I were to reference gamer['numYearsPlaying'], this would be 0, so if I used this reference in an if statement, it could have unexpected results (see below).

if (gamer.numYearsPlaying) {
  console.log(gamer.name + ' has been playing for ' + gamer.numYearsPlaying + '.');
}

The above console.log statement would not execute, because the if statement evaluates to false. Using hasOwnProperty avoids this issue.

if (gamer.hasOwnProperty('numYearsPlaying')) {
  console.log(gamer.name + ' has been playing for ' + gamer.numYearsPlaying + '.');
}

The above would display the following to the console:

Randell has been playing for 0 years.

1 Like

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution.

Thank you.

1 Like

Thanks! This breakdown really helpls!!!