-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.js
40 lines (33 loc) · 797 Bytes
/
12.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
var fs = require('fs');
function iterate(fn, object) {
if (!(object instanceof Array)) {
for (var item in object) {
if (object[item] === "red") {
return;
}
}
}
for (var item in object) {
if (object[item] !== null && (
typeof(object[item]) == "object" ||
Array.isArray(object[item])
)) {
iterate(fn, object[item]);
} else {
fn.call(this, item, object[item]);
}
}
}
fs.readFile( __dirname + '/input', function (err, data) {
if (err) {
throw err;
}
var parsed = JSON.parse(data.toString());
var sum = 0;
iterate(function(key, val) {
if (!isNaN(val)) {
sum += val;
}
}, parsed);
console.log(sum);
});