-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproblem-21.js
60 lines (54 loc) · 2.19 KB
/
problem-21.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
59
60
/* #Problem Task
A list of strings is given and each string will do following tasks:
x is an integer; enter a new x score.
"+" - Add the previous two scores together to create a new score. There will always be at least
two previous scores.
"D" - Make a new score that is twice as high as the previous one. There will almost certainly
be a previous score.
"R" - Remove the previous score from the record by invalidating it. There will almost certainly
be a previous score.
Return the total of all the records' scores.
Input: ops = ["7","-8","9","R","D","10","+","+"]
Output: -9
Explanation:
"7" - Increase the record by 7, making it [7].
"-8" - Increase the record by -8, making it [7, -8].
"9" - Increase the record by 9, making it [7, -8, 9].
"R" - Discard the previous score and replace it with [7, -8].
"D" - To the record, add 2* -8 = -16, making the record [7, -8, -16].
"10" - Increase the record by one, making it [7, -8, -16, 10].
"+" - Make the record [7, -8, -16, 10, -6 ] by adding -16 + 10 = -6.
"+" - Make the record [7, -8, -16, 10, -6, 4] by adding 10+ -6 = 4.
7 + -8 + -16 + 10 + -6 + 4 = -9 is the total. */
// existing Array
const ops = ['7', '-8', '9', 'R', 'D', '10', '+', '+'];
// update value from the loop of Array(ops)
let updateValues = [];
// traversing the Array(ops) and applying logics
for (let i = 0; i < ops.length; i++) {
// conditions & checkings
// check the string contains number or not
if (/^[-+]?[0-9]\d*(\.\d+)?$/.test(ops[i])) {
const parseToInt = parseInt(ops[i]);
updateValues.push(parseToInt);
}
// discard the previous score and replace
else if (ops[i] === 'R') {
updateValues.pop();
}
// multiply the last element of the Array(updateValues)
else if (ops[i] === 'D') {
const multiplyTheValue = updateValues[updateValues.length - 1] * 2;
updateValues.push(multiplyTheValue);
}
// make the record by adding [-16 + 10] & [10 + -6]
else if (ops[i] === '+') {
updateValues.push(
updateValues[updateValues.length - 2] +
updateValues[updateValues.length - 1]
);
}
}
// counting the final output
const finalResult = updateValues.reduce((a, b) => a + b, 0);
console.log(finalResult); // Output = -9