FreeCodeCampChina / freecodecamp.cn

FCC China open source codebase and curriculum. Learn to code and help nonprofits.

Home Page:https://fcc.asia/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

access the Object

zzusunjs opened this issue · comments

commented

Challenge Profile Lookup has an issue.
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.
Please describe how to reproduce this issue, and include links to screenshots if possible.

My code:

//初始化变量
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 lookUp(firstName, prop){
// 请把你的代码写在这条注释以下
  console.log(firstName, prop);
  for (var i=0; i<contacts.length; i++){
    if (contacts[i].firstName == firstName){
      if(contacts[i].hasOwnProperty(prop)){
        //console.log(contacts[i] , contacts[i].prop);
        return contacts[i].prop;
        
      }else{
        return "No such property";
      }
    }
  }
  return "No such contact";

// 请把你的代码写在这条注释以上
}

// 你可以修改这一行来测试你的代码
lookUp("Harry", "likes");

Question is I can't access the obj through return contacts[i].prop. But return contacts[i][prop] works.