我有一個問題,因爲我的代碼返回不同的結果比解決方案代碼。我發現不同之處在於過濾功能。雄辯的JavaScript 5.2母子
鏈接excersise:http://eloquentjavascript.net/code/#5.2
我的過濾功能:
ancestry.filter(function(p) { return p.mother != null; })
回報33的結果,而從溶液過濾函數只返回17:
ancestry.filter(function(person) { return byName[person.mother] != null;})
哪一個是正確的?爲什麼第二個函數返回不同的結果?
第二個問題:
我不知道怎麼拉出生日期映射功能的母親。
function ageDiff(p) { return p.born - p.mother.born } //p.mother.born doesn't work
感謝,KK
與你問的無關,'return(p.mother!= null)? true:false;'可以簡化爲'return(p.mother!= null)'。 – nnnnnn
謝謝:)代碼已更新。 – Kamil
_「爲什麼第二個函數返回不同的結果?」_因爲'p.mother!= null'與'byName [person.mother]!= null'不一樣 – Andreas