-
Notifications
You must be signed in to change notification settings - Fork 1
/
challenge-16.js
43 lines (40 loc) · 1.17 KB
/
challenge-16.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
'use strict';
function decodeNumber(symbols) {
var mapping = {
'.' : 1,
',' : 5,
':' : 10,
';' : 50,
'!' : 100,
};
var total = 0;
var prev = mapping[symbols[0]] || NaN;
if (isNaN(prev)) {
return NaN;
}
for (var i=0; i<symbols.length; i++) {
var curr = mapping[symbols[i]] || NaN;
if (isNaN(curr)) {
return NaN;
}
if (curr > prev) {
total += curr-prev-prev;
} else {
total += curr;
}
prev = curr;
}
return total;
}
console.log(decodeNumber('...')); // 3
console.log(decodeNumber('.,')); // 4 (5 - 1)
console.log(decodeNumber(',.')); // 6 (5 + 1)
console.log(decodeNumber(',...')); // 8 (5 + 3)
console.log(decodeNumber('.........!')); // 107 (1 + 1 + 1 + 1 + 1 + 1 + 1 - 1 + 100)
console.log(decodeNumber('.;')); // 49 (50 - 1)
console.log(decodeNumber('..,')); // 5 (-1 + 1 + 5)
console.log(decodeNumber('..,!')); // 95 (1 - 1 - 5 + 100)
console.log(decodeNumber('.;!')); // 49 (-1 -50 + 100)
console.log(decodeNumber('!!!')); // 300
console.log(decodeNumber(';!')); // 50
console.log(decodeNumber(';.W')); // NaN