简体   繁体   中英

Lookup function in javascript

I'm trying to solve a challenge and here's the problem set

We have an array of objects representing different people in our contacts lists.

A lookUpProfile function that takes firstName and a property (prop) as arguments has been pre-written for you.

The function should check if firstName is an actual contact's firstName and the given property (prop) is a property of that contact.

If both are true, then return the "value" of that property.

If firstName does not correspond to any contacts then return "No such contact"

If prop does not correspond to any valid properties then return "No such property"

My attempt

//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
for(var i =0;i<contacts.length;i++){
  if (contacts[i].firstName===firstName && contacts[i].hasOwnProperty(prop)===true){
    return contacts[i][prop];
  }
    else if (contacts[i].firstName===undefined){
return "No such contact";
    }
  else if (contacts[i].hasOwnProperty===false){
    return "No such property";
  }

}

// Only change code above this line
}

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

If I enter non-existent values into the lookUpProfile function it just returns undefined

eg lookUpProfile("Donald", "likes");

<. Undefined

Please help me fix this. You can also leave a comment on this gist

Thanks

Your first condition in loop should check if name is equal. If it satisfy Then you should check if the property exists If it exists return the value else return no such property .If No such name is found then it will come out of loop and you can return no such contact .

 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 for(var i =0;i<contacts.length;i++){ if (contacts[i].firstName===firstName){ if(contacts[i].hasOwnProperty(prop)===true){ return contacts[i][prop]; } else { return "No such property"; } } } return "No such contact"; } //No such contact console.log(lookUpProfile("Donald", "likes")); //No such property console.log(lookUpProfile("Sherlock", "locks")); //returns value of property console.log(lookUpProfile("Kristian", "likes"));

you can just simply resolve it like this:

const found = contacts.find(contact => contact.firstName == name)
return !found ? 'No such contact' : found[prop] || 'No such property'

Try following code:

function lookUpProfile(firstName, prop){

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

    // Only change code above this line
    }

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

Another way of doing this is by using OR operators. Then, only one "if statement" is needed":

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) {
    for (i = 0; i < contacts.length; i++) {
        if (contacts[i].firstName === firstName) {
            return contacts[i][prop] || "No such property";
        }
            return "No such contact";
    }
}

console.log(lookUpProfile("Akira", "likes")); // Returns [ 'Pizza', 'Coding', 'Brownie Points' ]

console.log(lookUpProfile("Akira", "current job")); // Returns No such property

console.log(lookUpProfile("Karanja", "likes")); // Returns No such contact

Here, the second part of the OR operator works as an "else" statement.

Came across this problem on FreeCodeCamp's Javascript Algorithms and Data Structures profile lookup problem myself and it seems the above answers are somewhat outdated.

Below is how I fixed it:

 function lookUpProfile(name, prop) { // Only change code below this line for (let i = 0; i < contacts.length; i++){ if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)) { return contacts[i][prop] } else if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop) == false) return "No such property" } return "No such contact" // Only change code above this line }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM