Javascript对象导致混淆

时间:2017-02-21 16:10:01

标签: javascript json

我在freecodecamp.com并遇到了Profile Lookup的问题。我在这里缺少什么?任何人都可以指出我吗?如果我发表评论firstName == contacts[i].firstName,它会返回4次没有此类联系& 1次没有这样的财产我做错了什么?

用例:

 - "Kristian", "lastName" should return "Vos" 
 - "Sherlock", "likes" should return ["Intriguing Cases", "Violin"]  
 - "Harry","likes" should return an array 
 - "Bob", "number" should return "No such contact"  //**fails**
 - "Akira","address" should 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 lookUpProfile(firstName, prop){
      // Only change code below this line
       for(var i = 0; i< contacts.length;i++){

         if(firstName == contacts[i].firstName && 
               contacts[i].hasOwnProperty(prop)){

             return contacts[i][prop];
         }

         /*if(firstName == contacts[i].firstName){
            return "No such contact" ;
         }*/ 

        if(contacts[i].hasOwnProperty(prop) === false){
           return "No such property";  
        }

       }
      }

       //test strings
       console.log(lookUpProfile("Kristian", "lastName"));
       console.log(lookUpProfile("Sherlock", "likes"));
       console.log(lookUpProfile("Harry", "likes"));
       console.log(lookUpProfile("Bob", "number"));
       console.log(lookUpProfile("Akira", "address"));

1 个答案:

答案 0 :(得分:1)

你可以移动

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) {
    for (var i = 0; i < contacts.length; i++) {
        if (firstName == contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
            return contacts[i][prop];
        }
        if (!contacts[i].hasOwnProperty(prop)) {
            return "No such property";
        }
    }
    return "No such contact"; // move to the end of the function
}

//test strings
console.log(lookUpProfile("Kristian", "lastName"));
console.log(lookUpProfile("Sherlock", "likes"));
console.log(lookUpProfile("Harry", "likes"));
console.log(lookUpProfile("Bob", "number"));
console.log(lookUpProfile("Akira", "address"));
.as-console-wrapper { max-height: 100% !important; top: 0; }