简体   繁体   English

我在这个javascript代码中哪里出错了?

[英]Where am I going wrong in this javascript code?

I'm in the 217th challenge of freecodecamp which is profile lookup. 我正处于freecodecamp的第217个挑战中,即查找配置文件。

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. 已经为您预先编写了一个以firstName和一个属性(prop)作为参数的lookUpProfile函数。 The function should check if firstName is an actual contact's firstName and the given property (prop) is a property of that contact. 该函数应检查firstName是否是实际联系人的firstName,并且给定属性(prop)是该联系人的属性。 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" 如果firstName与任何联系人不对应,则返回“No such contact”如果prop不对应任何有效属性,则返回“No such property”

I saw many using the equality operator within the "if" loop but I wanted to solve it using the "hasOwnProperty" function. 我看到很多人在“if”循环中使用了相等运算符,但我想使用“hasOwnProperty”函数来解决它。 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) . 您需要比较firstName属性的实际值, (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] 您没有引用循环contacts[i]的单个联系人
  2. fix the if statement to compare the first name 修复if语句以比较名字
  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. 问题是lookUpProfile函数中的firstName不是属性。 It is a value for firstName property. 它是firstName属性的值。

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. 当我们调用lookUpProfile("Akira", "likes") ,您的代码会尝试检查数据中是否存在属性“Akira”(它没有)并且它不会返回该属性。 You need to replace contacts.hasOwnProperty(firstName) in your if statement with contacts.firstName === firstName 您需要使用contacts.firstName === firstName替换if语句中的contacts.hasOwnProperty(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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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