我有一个问题,因为我的代码返回不同的结果比解决方案代码。我发现不同之处在于过滤功能。雄辩的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