Profile Lookup can not pass

Hello ! My can not pass challenge https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup/
What is wrong with my code

//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
var ar = "";
for (var i = 0; i< contacts.length ; i++) {
if (contacts[i]["firstName"] == name && contacts[i][prop] ) {
    console.log(contacts[i][prop]);
     ar = contacts[i][prop] ;

} else if (contacts[i]["firstName"] !== name   ) {
    ar = "No such contact" ;
     console.log(ar); 
} else  {
    ar = "No such property";
    console.log(ar);
    }
}
return ar ;
// Only change code above this line
}

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

"Kristian", "lastName" should return "Vos"

"Sherlock", "likes" should return ["Intriguing Cases", "Violin"]

"Harry","likes" should return an array

Passed

"Bob", "number" should return “No such contact”

Passed

"Bob", "potato" should return “No such contact”

"Akira", "address" should return “No such property”

Hey @Quintis, it looks like you’re proceeding with your loop even after you’ve got firstName match, so your ar variable gets reassigned :slight_smile:

and how to fix it?) What I must to change in my code ?

You can use break to stop a loop, but careful on where you put it

you mean switch case break operators ?

I have made it))))) but how optimize my code ?

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

// Change these values to test your function
lookUpProfile("Bob", "number");

Imagine that each object in your array is a paper card and array itself is a stack of these cards laying on the table in front of you? How would you solve it? I personally would find a card with given firstName and then I have 3 options:

  1. If no such card: "No such contact"
  2. If no property on the card: "No such property"
  3. Read the value of the given property

Now, if it sounds like a good plan to you, try to implement it in the code (using Array.prototype.find())

Thank you)
:ухмыляющийся:

I have made quiz))))) but how optimize my code ?

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

// Change these values to test your function
lookUpProfile("Bob", "number");

What is the link to the challenge, what errors are you getting / what are you not passing. What does the console log.

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

I have no errors, just want to see how optimized my code