-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitem_46.js
More file actions
69 lines (54 loc) · 1.98 KB
/
item_46.js
File metadata and controls
69 lines (54 loc) · 1.98 KB
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
// Prefer arrays to dictionaries for ordered collections
// intuitively JavaScript objects are an unordered collection of properties
function report(highScores) {
var result = "";
var i = 1;
for (var name in highScores) { //unpredictable order
result += i + ". " + name + ": " + highScores[name] + "\n";
i++;
}
return result;
}
report({Nick: 16, Son: 32, Kaigi: 64});
// "1. nick: 16\n2. Son: 32\n3. Kaigi: 64\n"
// different envs may choose to store and enumerate the properties of the object
// in different orders
// If you need to depend on the order of entries in a data structure use an array!
function report(highScores) {
var result = "";
for (var i = 0, n = highScores.length; i < n; i++) {
var score = highScores[i];
result += (i + 1) + ". " + score.name + ": " + score.points + "\n";
}
return result;
}
report([{name: "Nick", points: 16 },
{name: "Son",points: 32 },
{name: "Kaigi", points: 64}]);
// "1. Nick: 16\n2. Son: 32\n3. Kaigi: 64\n"
// another PITA is floating-point arithmetic
var ratings = {
"The Blacklist": 0.8,
"Warrior": 0.7,
"Game of Thrones S8": 0.6,
"Chernobyl": 0.9
};
// recall in item_2 rounding in floating-point arithmetic can lead to subtle
// dependencies on the order of operations. Combined with undefined order of
// enumeration, can lead to unpredictable loops
var total = 0, count = 0;
for (var key in ratings) { // unpredictable order
total += ratings[key];
count++;
}
total /= count;
total; // 0.75 in my env
// lets play around with the order
var total1 = (0.8 + 0.7 + 0.6 + 0.9) / 4; // 0.75
var total2 = (0.6 + 0.8 + 0.7 + 0.9) / 4; // 0.7499999999999999
// in this case, best use integer values because integer addition can be
// performed in any order
// The sensitive division operations are performed only at the very end
// after the loop is complete
var total3 = (8 + 7 + 6 + 9) / 4 / 10; // 0.75
var total4 = (6 + 8 + 7 + 9) / 4 / 10; // 0.75