-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimmutability.js
58 lines (50 loc) · 1.55 KB
/
immutability.js
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
(function() {
function ready(fn) {
if (document.readyState != 'loading'){
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
function formatMetrics(metrics){
var oneThousand = 1000;
var oneMillion = oneThousand * oneThousand;
var oneBillion = oneMillion * oneThousand;
metrics.forEach(function(metric){
if (metric.value >= oneBillion) {
metric.value = Math.floor(metric.value / oneBillion);
return;
}
if (metric.value >= oneMillion) {
metric.value = Math.floor(metric.value / oneMillion);
return;
}
});
return metrics;
}
function showInTable (metrics, table) {
metrics.forEach(function(metric){
var tr = document.createElement("tr");
var tdValue = document.createElement("td");
var tdYear = document.createElement("td");
tdValue.textContent = metric.value;
tdYear.textContent = metric.year;
tr.appendChild(tdValue);
tr.appendChild(tdYear);
table.appendChild(tr);
});
}
ready(function(){
var metrics = [
{value: 210394680, year: 2010},
{value: 740276402, year: 2011},
{value: 986737865, year: 2012},
{value: 872356504, year: 2013}
];
var formattedMetrics = formatMetrics(metrics);
var formattedMetricsTable = document.getElementById("formatted-metrics");
var originalMetricsTable = document.getElementById("original-metrics");
showInTable(formattedMetrics, formattedMetricsTable);
showInTable(metrics, originalMetricsTable);
});
}());