今天在 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"ui
//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",再也不繼續查找數組。this
先將全部firstName存入一個數組,判斷firstName是否存在,若是不存在,則直接返回"No such contact",若是存在,則再引入for循環,在for循環中判斷是否存在prop.code
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 }
寫文章先在本地寫,而後再複製到網頁中(不當心按到返回,也是沒誰了,多花了20分鐘重寫)。ip
循環還要多多練習。it
與object.hasOwnProperty不一樣,數組須要判斷是否存在值時,可引入 contains 方法:io
function contains(arr, obj) { var i = 0; while (i < arr.length) { if (arr[i] === obj) { return true; } i++; } return false; }
今早起來纔想起來,把return 「No such contact」放在 for循環外,等for循環結束找不到firstName,會直接返回 「No such contact」.for循環
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.ast