Profile Lookup javascript alternative solutions?

Tell us what’s happening:

Your code so far

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(name === contacts[i]["firstName"]){  
      return contacts[i][prop];
      return "No such property";  
     return "No such contacts";

// 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 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:

I don’t know what’s wrong with my code.
I combined two if statement using && at first, but changed it to use else statement.
Anyway either one doesn’t work.
can anyone gimme a solution and possibly show me an answer
using lots of else if statement , not just simplified one like original solution?

change “firstName” in conditional to firstName(without quotes)

I did it but nothing’s happened.

did you tried it removing square brackets also?
like contacts[i].firstname

only “Akira”, “address” should return “No such property” runs.

You have syntax errors.

You have several extra { brackets that don’t mach with your function.

for example here:

      return contacts[i][prop];

notice that you have two closing brackets.

Also the program expects you to return

"No such contact"

While you are returning

"No such contacts"

Once fixed this syntax error I was able to pass the challenge with your code

Thank you! By the way, is there any way that i can pass the test with combined
if statement?
Is there specific reason why not to do this or is it just fine either way?

if(name === contacts[i]["firstName"]&&  
      return contacts[i].prop;}

this is what i’d done before i tried the one i was able to pass the test.

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

The problem is not chaining if else or conditional &&, but the logic that your function executes:

Imagine I am looking for:

"Kristian" "lastName"

Which is the last in our record.

Your code will start looping and the first one is “Akira Laine”.
What happens now is that your code:

 if(name === contacts[i]["firstName"]&&  
  contacts[i].hasOwnProperty(prop)) // false

// move to next condition
if(name !==contacts[i]["firstName"]) // true

Well this is true: "Akira !== Kristian" so the program returns:

"No such contact"

But as a mattar of fact it’s there, and has even a valid prop :slight_smile:
Hope it helps

1 Like

So I’ve ended up looking up the right solution and the for loop goes

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

return "No such contact";

I don’t understand whyreturn "No such contact"; should be outside of the for loop and how it’s working since there’s no if statement to check if the contacts array has name parameter in the function. :frowning:
shouldn’t it automatically run return "No such contact"; no matter what if it’s not nested inside of if statement?

Because you only know a person’s firstName does not appear in the contacts array until you have iterated through the entire array. If the name would have appeared, you would have already returned either the the prop value or “No such property”.

FYI - 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.

1 Like

Thank you!!! :heart_eyes::heart_eyes::heart_eyes: