-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperations.js
More file actions
30 lines (26 loc) · 781 Bytes
/
Operations.js
File metadata and controls
30 lines (26 loc) · 781 Bytes
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
/*** Given an array of strings operations containing a list of operations,
* return the final value of X after performing all the operations.*/
// solution:
var finalValueAfterOperations = function (operations) {
let x = 0;
for (let i in operations) {
if (operations[i] == "X++" || operations[i] == "++X") {
x++;
} else {
x--;
}
}
return x;
};
// another way
var finalValueAfterOperations = function (operations) {
let x = 0;
for (let i in operations) {
operations[i] == "X++" || operations[i] == "++X" ? x++ : x--;
}
return x;
};
//operations = ["--X", "X++", "X++"];
operations = ["X++", "++X", "--X", "X--"];
//operations = ["++X", "++X", "X++"];
console.log(finalValueAfterOperations(operations));