Profile Lookup ReferenceError: prob is not defined

Profile Lookup ReferenceError: prob is not defined
0.0 0

#1

Tell us what’s happening:
i don’t understand why i m getting this error ReferenceError: prob is not defined
can you pls help me to understand it?

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

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/66.0.3359.181 Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:


#2

Seems you have a typo in there:

within your function you use the variable prob:

 if (contacts[i].hasOwnProperty(prob)){

but your function is declared using prop:

function lookUpProfile(firstName, prop){

…change this to prob and it should work :wink:


#3

what a funny :slight_smile: from yesterday night . i m looking it.
can you tell me why this function wrong.

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

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

#4

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Walk through your code for the test case of lookUpProfile(“Kistian”, “lastName”) and you will see that in the first iteration (where i = 0), your outer if statement will evaluate to false, because firstName (which is “Kristian”) is not equal to contacts[0].firstName (which is “Akira”), so your else block of code returns “No such contact”.

Remember, when the return statement is executed, the function is exited and does not return to continue iterating through the for loop.


#5

You are only checking the first element for a name match. If that match fails then function returns with message “No such contact”. So unless the contact you are searching for happens to be the first one your function doesn’t work.

You need to check all elements for a name match, then if you find one, proceed checking that element for a property match.

Good luck.


#6

thank u very much :slight_smile: i got it