-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathmake-the-deadfish-swim.js
47 lines (43 loc) · 1.14 KB
/
make-the-deadfish-swim.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
function parse(data) {
let result = 0;
let output = [];
data.split('').forEach((operation) => {
if (operation === 'i') {
// i increments the value (initially 0)
result += 1;
} else if (operation === 'd') {
// d decrements the value
result -= 1;
} else if (operation === 's') {
// s squares the value
result **= 2;
} else if (operation === 'o') {
// o outputs the value into the return array
output.push(result);
}
});
return output;
}
function parse(data) {
return data.split('').reduce(({ result, output }, operation) => {
if (operation === 'i') {
// i increments the value (initially 0)
result += 1;
} else if (operation === 'd') {
// d decrements the value
result -= 1;
} else if (operation === 's') {
// s squares the value
result **= 2;
} else if (operation === 'o') {
// o outputs the value into the return array
output.push(result);
}
return { result, output };
}, {
result: 0,
output: [],
}).output;
}
console.log( parse('iiisdoso'), [ 8, 64 ] );
console.log( parse('iiisxxxdoso'), [ 8, 64 ] );