解法:foreach走訪,透過indexOf判斷為--,則數值減1;反之加1
JavaScript
/**
* @param {string[]} operations
* @return {number}
*/
var finalValueAfterOperations = function (operations) {
let x = 0;
operations.forEach(s => {
if (s.indexOf("--") >= 0) {
x -= 1;
} else {
x += 1;
}
})
return x;
};