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

时间:2016-08-09 18:10:48

标签: javascript

我正处于freecodecamp的第217次挑战中,即查找配置文件。

这是问题定义

我们的联系人列表中有一组代表不同人的对象。 已经为您预先编写了一个以firstName和一个属性(prop)作为参数的lookUpProfile函数。 该函数应检查firstName是否是实际联系人的firstName,并且给定属性(prop)是该联系人的属性。 如果两者都为真,则返回该属性的“值”。 如果firstName与任何联系人不对应,则返回“No such contact” 如果prop不对应任何有效属性,则返回“No such property”

我看到很多人在“if”循环中使用了相等运算符,但我想使用“hasOwnProperty”函数来解决它。我不知道我哪里出错了。

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");

7 个答案:

答案 0 :(得分:1)

您需要比较firstName属性的实际值(contacts[i].firstName == firstName)

在评论中查看更多详情。

这是有效的code

&#13;
&#13;
// 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"));
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你只是忘了查找数组中的对象,而不是整个数组:

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";

}

答案 2 :(得分:0)

  1. 您没有在循环contacts[i]
  2. 中引用单个联系人
  3. 修复if语句以比较名字
  4. 您必须从下面的变量中获取属性值
  5. &#13;
    &#13;
    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");
    &#13;
    &#13;
    &#13;

答案 3 :(得分:0)

问题是firstName lookUpProfile函数不是属性。它是firstName属性的值。

当我们致电lookUpProfile("Akira", "likes")时,您的代码会尝试检查属性&#34; Akira&#34;存在于数据中(不存在)并且它不返回属性。您需要使用contacts.hasOwnProperty(firstName)

替换if语句中的contacts.firstName === firstName

此外,您需要在循环中联系:

var contact = contacts[i];

答案 4 :(得分:0)

上述代码适用于一个测试用例,但不适用于通过会话所需的所有测试用例。

    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";
    }

答案 5 :(得分:0)

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";
        }
    }

答案 6 :(得分:-1)

};

MapControl.prototype.componentDidMount = function componentDidMount() {
-> this.leafletElement.addTo(this.props.leaflet.map);
};

MapControl.prototype.componentDidUpdate = function componentDidUpdate(prevProps) {