-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path227.basic-calculator-ii.js
More file actions
55 lines (47 loc) · 1.49 KB
/
227.basic-calculator-ii.js
File metadata and controls
55 lines (47 loc) · 1.49 KB
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
/**
* @param {string} s
* @return {number}
*/
var calculate = function(s) {
if (s.length === 0) {
return 0;
}
// The first operator will always be implicitly '+'
// since "string contains only non-negative integers".
let op = "+";
// this is kind of an "addition stack". the numbers in this stack
// is sum'ed in the end.
let stack = [];
// n is the current digit. loop 1 more time than
// the length of the string ('<='), to push the last digit
// to the stack
for (let i = 0, operand = 0; i <= s.length; ++i) {
let currentCharacter = s.charAt(i);
// always skipp white space
if (currentCharacter === " ") continue;
// aggregate the digits between each operator
// into 'n'.
if (currentCharacter >= "0" && currentCharacter <= "9") {
console.log({ multiple: operand });
operand = operand * 10 + parseInt(currentCharacter);
continue;
}
//calculating the
// we have now reached a non-digit character (an operator),
// time to handle 'n', depending on the last seen operator.
if (op === "+") {
stack.push(operand);
} else if (op === "-") {
stack.push(-operand);
} else if (op === "*") {
stack.push(stack.pop() * operand);
} else if (op === "/") {
stack.push(Math.trunc(stack.pop() / operand));
}
// c must be and operator, so store it for the next number.
op = currentCharacter;
operand = 0;
}
// return the sum of the stack.
return stack.reduce((n, acc) => n + acc, 0);
};