简体   繁体   中英

Where am I going wrong in this javascript code?

I'm in the 217th challenge of freecodecamp which is profile lookup.

This is the problem definition

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"

I saw many using the equality operator within the "if" loop but I wanted to solve it using the "hasOwnProperty" function. I don't know where I'm going wrong.

 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.hasOwnProperty(firstName) && contacts.hasOwnProperty(prop)) { return contacts.prop; } return "No such property"; } // Only change code above this line } // Change these values to test your function lookUpProfile("Akira", "likes"); 

You need to compare actual value of firstName property, (contacts[i].firstName == firstName) .

See more details in comments.

Here is working code

 // Code goes here 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++){ // use i as array index, to access particular contact object if((contacts[i].firstName == firstName) && contacts[i].hasOwnProperty(prop)){ //dot notation will not work here, use [] return contacts[i][prop]; } return "No such property"; } // Only change code above this line } // Change these values to test your function console.log(lookUpProfile("Akira", "likes")); 

you just forgot to lookup for the object in your array, and not your entire array :

for(var i=0; i<contacts.length; i++) {

    var contact = contacts[i];

    if(contact.hasOwnProperty('firstName') && contact.hasOwnProperty(prop)) {
        return contact.prop;
    }

    return "No such property";

}
  1. You are not referencing the single contact in the loop contacts[i]
  2. fix the if statement to compare the first name
  3. You have to get the property value from a variable like below

 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)) { //return contacts[i].prop; alert(contacts[i][prop]); } //return "No such property"; alert("No such property"); } // Only change code above this line } // Change these values to test your function lookUpProfile("Akira", "likes"); 

The issue is firstName in lookUpProfile function is not a property. It is a value for firstName property.

when we call lookUpProfile("Akira", "likes") , your code tries to check if the property "Akira" exist in the data (which does not) and it does not return the property. You need to replace contacts.hasOwnProperty(firstName) in your if statement with contacts.firstName === firstName

In addition, you need to get contact in your loop:

var contact = contacts[i];

The above code will work for one test case but not for all that are required to pass the session.

    function lookUpProfile(firstName, prop){
    for (i = 0; i < contacts.length; i++) {
        if (contacts[i].hasOwnProperty(prop)) {
            if (contacts[i].firstName == firstName) {
                return contacts[i][prop];
            }
        }
        else {
            return "No such property";
        }
    }
    return "No such contact";
    }

 Here is the solution to this problem function lookUpProfile(firstName, prop){ for (var i = 0; i < contacts.length; i++) { if (contacts[i].firstName == firstName && prop in contacts[i]) { return contacts[i][prop]; } else if (!(firstName in contacts[i]) && i == (contacts.length - 1)) { return "No such contact"; } else if ((typeof contacts[i][prop] == 'undefined')) { return "No such property"; } } 

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 printValues(){
    for(var a = 0; a< contacts.length; a++){
        contacts[a].firstName;
    }   
}

function isNameExist(name ){
    for(var a = 0; a< contacts.length; a++){
        if (contacts[a].firstName == name)
            return true
    }
    return false;
}

function isPropertyExist(prop){
    for(var a = 0; a< contacts.length; a++){
        if (contacts[a].hasOwnProperty (prop))
            return true
    }
    return false;
}

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

        if(!isNameExist(name)){
            return "No such contact";
        }else if(!isPropertyExist(prop)){
            return "No such property";
        }

         for(var a = 0; a< contacts.length; a++){
        if(contacts[a].firstName == name && contacts[a].hasOwnProperty(prop)){
                return contacts[a][prop];
            }   
        }
}
// Only change code above this line

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

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