今天在 FreeCodeCamp 第215步上花了1个多小时,好在最后完成了,现把问题和code记录下来。

题目:

Profile Lookup
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"

//Setup
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

// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");
lookUpProfile("Kristian", "lastName");
lookUpProfile("Sherlock", "likes");
lookUpProfile("Harry", "likes");
lookUpProfile("Bob", "likes");
lookUpProfile("Bob", "address");

遇到问题:

一开始把firstName和prop的判断写在了一个for循环下,结果导致如果firstName不是在第一个object中,则直接返回"No such contact",不再继续查找数组。

解决方法:

先将所有firstName存入一个数组,判断firstName是否存在,如果不存在,则直接返回"No such contact",如果存在,则再引入for循环,在for循环中判断是否存在prop.

function lookUpProfile(firstName, prop){
// Only change code below this line
//将所有firstName存入数组
  var storeFirstName = []; 
for (var i = 0; i < contacts.length; i++){
  
    storeFirstName.push(contacts[i].firstName); 

} 
//创建方法,用来判断数组中是否存在某个值     
  function contains(arr, obj) {  
    var i = 0;  
    while (i < arr.length) {  
        if (arr[i] === obj) {  
          
            return true;
        }
      i++;
    }  
    return false;  
}       

  
if (contains(storeFirstName, firstName))//firstName 存在于contacts中
  {
    for (var j = 0; j < contacts.length; j++){
      
      if(contacts[j].firstName === firstName) { //找到目标firstName所在的object            
        if (contacts[j].hasOwnProperty(prop)){ //object中存在目标prop
        
          return contacts[j][prop];
          
        }
        else {
          
          return "No such property"; //object中不存在目标prop
        }
      }
     
      
    }
  }
  else {  //firstName 不存在于contacts中
    return "No such contact"; 
  }
// Only change code above this line
}

心得:

  1. 写文章先在本地写,然后再复制到网页中(不小心按到返回,也是没谁了,多花了20分钟重写)。

  2. 循环还要多多练习。

  3. 与object.hasOwnProperty不同,数组需要判断是否存在值时,可引入 contains 方法:

function contains(arr, obj) {  
        var i = 0;  
        while (i < arr.length) {  
            if (arr[i] === obj) {  
              
                return true;
            }
          i++;
        }  
        return false;  
    }

2017.02.14

How stupid am I !

今早起来才想起来,把return “No such contact”放在 for循环外,等for循环结束找不到firstName,会直接返回 “No such contact”.

function lookUpProfile(firstName, prop){
// Only change code below this line
 for( var i = 0; i < contacts.length; i++) {
   
   if(contacts[i].firstName === firstName) {
     
     if(contacts[i].hasOwnProperty(prop)) {
       
       return contacts[i][prop];
     }
     else {
       
       return "No such property";
     }
   }
 
 }
  return "No such contact";
// Only change code above this line
}

nested 判断中,最外部的判断中false的情况,可以直接在最后。前提是之前的判断中的其他情况会terminate function.


codetime2017
1 声望1 粉丝