-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.html
77 lines (71 loc) · 2.25 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html>
<head>
<title>Getting started - Underscore.map.reduce and revealing module pattern</title>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>
</head>
<body>
<div>Example output:</div>
<div id="output"></div>
<script>
var awardAgeCalculator = (function() {
var getPeople = function() {
return [{
name: "Herta Muller",
birthYear: 1953,
awardYear: 2009
}, {
name: "Mario Vargas Llosa",
birthYear: 1936,
awardYear: 2010
}, {
name: "Tomas Transtromer",
birthYear: 1931,
awardYear: 2011
}, {
name: "Mo Yan",
birthYear: 1955,
awardYear: 2012
}, {
name: "Alice Munro",
birthYear: 1931,
awardYear: 2013
}, {
name: "Patrick Modiano",
birthYear: 1945,
awardYear: 2014
}];
};
var innerGetPeopleWithAwardAge = function() {
return _.map(getPeople(), function(person) {
return {
name: person.name,
awardAge: person.awardYear - person.birthYear
};
});
};
return {
getPeopleWithAwardAge: innerGetPeopleWithAwardAge,
getAverageAwardAge: function() {
var peopleWithAwardAge = innerGetPeopleWithAwardAge();
var totalAwardAge = _.reduce(peopleWithAwardAge, function(memo, person) {
return memo + person.awardAge;
}, 0);
return totalAwardAge / peopleWithAwardAge.length;
}
};
}());
$(document).ready(function() {
var outputContent = "<br />Award age for people:";
_.each(awardAgeCalculator.getPeopleWithAwardAge(), function(person) {
outputContent += "<br />";
outputContent += " - " + person.name + " was " + person.awardAge + " years old";
});
var averageAwardAge = Math.floor(awardAgeCalculator.getAverageAwardAge());
outputContent += "<br /><br />" + "Average award age is " + averageAwardAge + " years old.";
$("#output").html(outputContent);
});
</script>
</body>
</html>