Skip to content

Commit 23cf049

Browse files
committed
Fix countAncestors function in Chapter 5
It shouldn't count the person itself.
1 parent bebb069 commit 23cf049

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

05_higher_order.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -816,18 +816,18 @@ machine that is me that originates with Pauwels.
816816
function)))(((abstraction)))We could also have computed this number
817817
without relying on `reduceAncestors`. But separating the general
818818
approach (condensing a family tree) from the specific case (computing
819-
shared DNA) can improve the clarity of the code and allows us to
820-
reuse the abstract part of the program for other cases. For example,
821-
the following code finds the percentage of known ancestors, for a
822-
given person, who lived past 70:
819+
shared DNA) can improve the clarity of the code and allows us to reuse
820+
the abstract part of the program for other cases. For example, the
821+
following code finds the percentage of a person's known ancestors who
822+
lived past 70 (by lineage, so people may be counted multiple times):
823823

824824
// test: clip
825825

826826
[source,javascript]
827827
----
828828
function countAncestors(person, test) {
829-
function combine(person, fromMother, fromFather) {
830-
var thisOneCounts = test(person);
829+
function combine(current, fromMother, fromFather) {
830+
var thisOneCounts = current != person && test(current);
831831
return fromMother + fromFather + (thisOneCounts ? 1 : 0);
832832
}
833833
return reduceAncestors(person, combine, 0);
@@ -842,7 +842,7 @@ function longLivingPercentage(person) {
842842
return longLiving / all;
843843
}
844844
console.log(longLivingPercentage(byName["Emile Haverbeke"]));
845-
// → 0.145
845+
// → 0.129
846846
----
847847

848848
Such numbers are not to be taken too seriously, given that

0 commit comments

Comments
 (0)