Please help with Profile Lookup challenge

Please help with Profile Lookup challenge
0

#1

Tell us what’s happening:

I am having isses using an else statement with a for loop. I dont know how to allow the for loop to serve its purpose and then run an else statement or seperate if statement to meet the other criteria of the challange.

Your code so far

for(i=0; i<contacts.length; i++){
if(firstName == contacts[i].firstName){
if (contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}
}
}

//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(firstName, prop){
// Only change code below this line

  
for(i=0; i<contacts.length; i++){
  if(firstName == contacts[i].firstName){
  if (contacts[i].hasOwnProperty(prop)){
    return contacts[i][prop];
  }
  }
  }  
  
  
  
  
  
  
// Only change code above this line
}

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

Your browser information:

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

Link to the challenge:


#2

//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(firstName, prop){
// Only change code below this line

  
for(i=0; i<contacts.length; i++){
  if(firstName == contacts[i].firstName){
  if (contacts[i].hasOwnProperty(prop)){
    return contacts[i][prop];
  }
  }
  }  
  
  
  
  
  
  
// Only change code above this line

#3

So, this is your code:

function lookUpProfile(firstName, prop){
  for(i=0; i<contacts.length; i++){
    if(firstName == contacts[i].firstName){
      if (contacts[i].hasOwnProperty(prop)){
        return contacts[i][prop];
      } 
    }
  }  
}

OK, so what you’re missing are the last two directions:

  • If firstName does not correspond to any contacts then return “No such contact”
  • If prop does not correspond to any valid properties then return “No such property”

So, at some point in your code, you’re going to need:

return "No such contact";

and at some other point:

return "No such property";

The rest of your code is fine. You just need to decide where to insert these lines. One of them could use an else statement, but could also work without it.

Where would you put these? Trace though the logic and see if you can figure it out. These are big hints. Try it and check back if you get stuck again. You’re almost there.


#4

I after thinking about this for a little while this is the solution that I came up with. I am willing to bet that there is a simpler solution if I understood this a little more. The problem I was having trouble wrapping my head around was as soon as there is a return then no other code is considered. also you have to make sure the loop completes before you place the “no name” “no prop” arguments or it will return after checking the first object… I hope this makes sense. Thanks for the help.

function lookUpProfile(firstName, prop){
// Only change code below this line

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

#5

The problem I was having trouble wrapping my head around was as soon as there is a return then no other code is considered

Yes, and not just “not considered” but not even read. The whole point of the return is saying "I’m done with this function and here’s the value I’m sending back.

also you have to make sure the loop completes before you place the “no name” “no prop” arguments or it will return after checking the first object…

Well, there are ways to deal with that. For example, you don’t really need the variables x and y. Inside the if statement that checks is the names match, if contacts[i].hasOwnProperty(prop) then you return the value. Good. But if that is false, then then you can return “No such property” as an else to that. And if you get to the end of the for loop (after it, outside of it) and have not returned yet, then then you know that that contact isn’t there and you can return “No such contact”.

Your code works, but yes, there are a few ways to make it a little simpler.


#6

Thank you sir. I appreciate your feedback. I have a lot to learn here.


#7

We all do, that’s why we’re here. Just keep moving forward. You’ll get there eventually.