Profile Lookup hints

Profile Lookup hints
0

#1

Tell us what’s happening:

I believe my if else statements are correct to meet the conditions of the challenge but the loop won’t work. And I don’t think I have to use nested loops because if I do this:
for (var i=0; i < contacts.length; i++) { console.log(contacts[i].firstName); }

This will print the first name of each object, which I am trying to use as my condition.

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

// Change these values to test your function
lookUpProfile("Akira", "likes");

Your browser information:

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

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


#2

As soon as a return statement executes, the function exits immediately and does not come back to finish a for loop. Your for loop only makes one iteration before exiting the function.


#3

Ummm I see… Yes that’s true, so I must choose another path to solve this challenge.


#4

Plus, why would you return “No such contact” just because a contact had a particular property?

Try writing out your algorithm in plain language first before trying to write the code for it. Make sure you can walk through the algorithm on paper and it passes the tests.


#5

If the name doesn’t exist in any of the objects I have to return “No such contact”, hasOwnProperty evaluates to true or false whether the obj has the specified property or not, but I was missing the comparison, so if it’s false then return “No such contact”.

arr.hasOwnProperty === false {...}

I am not sure if that’s what you meant but I think it fixes my mistake.


#6

When would you know for sure that the name does not exist in any of the objects?


#7

When I call the function with the arguments.

lookUpProfile("John", "likes")

This should return “No such contact” because John doesn’t exist in any object.


#8

That is correct, but that was not what I was asking.

What question you should ask first when iterating through the contacts array (inside the for loop)?


#9

Well this was a great hint, I’m not sure if I came up with the best solution but at the end I managed to solve it after some struggle and taking a break:

function lookUpProfile(name, prop){

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

};

However there’s something that it’s not very clear to me, I know my original code has a few mistakes but why the loop will not check the first if statement, instead it will return “No such property”, so it goes straight to the else block w/o checking the previous conditions?


#10

I am not sure what you mean. Referring to your originally posted solution, the if statement condition would be evaluated first. If it evaluates to false, then the else if gets evaluated. If it evaluates to false, then the else statement code block would execute. Which condition evaluates to true or false would depend on the test case. Either way, your original function only iterated a single time, because during the first pass, you returned something which caused the function to exit.


#12

Ummmm, I think I get it now, basically it’s the order on which I stated my conditions. But I’m still trying to understand why my first code iterates only once and exits the function.

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

If the following conditions are not met
if(contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) { return contacts[i][prop];
Then the block of code within this statement should never be executed and continue with the loop.

edit: Ohhhh never mind, it’s because the else statement at the end of the loop… That’s why it only iterates once if none of the previous conditions are met. My other code works because I instinctively put “No such contact” condition out of the loop. Following your advice I thought the only way to know if there’s no contact with such name was after finishing iterating the loop.

Wow it felt like such a guess work but it’s so obvious now. On the other hand I had no idea I could use if else statements like that, I originally thought I’d need an if statement condition to initiate the loop and then someif else nested statements within the loop. I guess that could be another approach.

Well, it’s clear to me now, thanks a lot!


#13

Yes, that is correct if your if and else if code block use a return statement.