Profile Lookup with JavaScript

Tell us what’s happening:
Could some body please help me with this? i dont know what wrong with my code. it pass the last 3 rule but cant pass the other. why when i log “Sherlock” they sent “no such contact” ?

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

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

Your browser information:

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

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

The first thing you check is to see if contacts[0].firstName === name anything other than name === 'Akira' will return false and boot you to “No such contact”

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

for(var i=0;i<contacts.length;i++)
{
if(contacts[i].firstName === name)
{

if(contacts[i].hasOwnProperty(prop))
{
  alert(contacts[i][prop]);
  return contacts[i][prop];
}
else{
  return  "No such property";
}

}
}
return “No such contact”;
// Only change code above this line
}

there was a problem in if condition i have corrected it check it out

yeah . if i change any thing else “Akira” it return “no such contracts”. what i should do ?

Think about the flow of logic. What would be the most broad category of true and false you could check for?

Okay, since it’s Christmas, your solve is very easy: remove the

else {
  return  "No such contact";
}

completely, then put

return "No such contact";

completely outside of your for loop:

/*code up here
*
*
*/
  }
}
return "No such contact";
// Only change code above this line
}

What is happening right now, is the first time your loop comes to a name that doesn’t match the name parameter, it is defaulting to the return "No such contact"; as the last instruction in the loop before starting over. What you want it to do is loop through all the choices and only return "No such contact"; once it has exhausted all the possible contacts.

1 Like