2016年1月24日 星期日

freeCodeCamp #224 Profile Lookup



昨天在這關卡了我大概至少有8個小時左右的時間(遠目)

進到chat room有個外國人跟我一樣為了這題crazy for hours XDD  因為英文太爛,我一直在讓他發問等人解救

相信有人因為邏輯概念,會卡在相同的一題,忍不住來記錄一下,讓我搞到半夜三點才睡的邏輯問題

以下是讓人花了好幾個小時讓人跳不出來歪掉的logic

function lookUp(firstName, prop){

for (var i=0; i<contacts.length ;i++){  
  if(firstName==contacts[i].firstName && contacts[i].hasOwnProperty(prop)){
    return contacts[i][prop];
  }
   else if (firstName!=contacts[i].firstName){
  return "No such contact"; 
}
  else if (!contacts[i].hasOwnProperty(prop)){
    return "No such property";
  }
}


然後有好心人提供以下正確的邏輯,終於逃脫出來惹~~(灑花)

```
function lookUp(firstName, prop){
// Only change code below this line
  //loop through all records{
    // if name a match?{
       //check if property exists{
                // if property exists return property
                // else if property does not exists return 'no property'  
            }
        }
        // if name not match do nothing - go check next record
    }

  // if all else fails return 'no contact',
  //you will only get here if no returns happened previously

}
```


通常在成功submit後,會出現像You're on fire!或是Walking on the warm sunshine.之類的鼓勵語

但是這題pass後跳出來的是


""Look at you! Code!""


............喵的XD,掰掰惹我的8小時!!~(揮手拭淚)







沒有留言:

張貼留言