Profile Lookup doesn't return "value" and only return "no such contacts"

Profile Lookup doesn't return "value" and only return "no such contacts"
0

#1

Tell us what’s happening:

I’ve solving this problem
My function code doesn’t return value, and return only “No such contact”

when I try this function on console.

  1. lookUpProfile(“Kristian”, “lastName”);
    return -> “No such contact” (it should be “Vos”)

number 2,3 are same problem, it doesn’t return value even if condition is satisfied.

2.lookUpProfile(“Sherlock”, “likes”);
“No such contact”
3.lookUpProfile(“Harry”,“likes”);
“No such contact”

So I tried this “If condition” on console
contacts[3][“firstName”] == “Kristian” && contacts[3].hasOwnProperty(“lastName”)
But It doesn’t have error It return
true

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){

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

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

Your browser information:

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

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


#2

Your code always returns a value in the first iteration of the for loop. Why? Because as soon as the return statement is executed in the for loop, the function immediately exits and does not return to finish the loop.


#3

Oh Thanks I solved problem