DEV Community 👩‍💻👨‍💻

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Baseball Game

/**
 * @param {string[]} operations
 * @return {number}
 */
var calPoints = function(operations) {
    let arr = [];
    let result = 0;
    for (let i of operations) {
        if (i === "D") {
            let lastElement = parseInt(arr[arr.length - 1]);
            let double = lastElement * 2;
            arr.push(double);
            result += double;
        }

        else if (i === "C") {
            let popElement = arr.pop();
            result -= popElement;
        }
        else if (i === "+") {
            let firstLastElement = parseInt(arr[arr.length - 1]);
            let secondLastElement = parseInt(arr[arr.length - 2]);
            arr.push(firstLastElement + secondLastElement);
            result += (firstLastElement + secondLastElement);
        }
        else {
            let elementInt = parseInt(i);
            arr.push(elementInt);
            result += elementInt;
        }
    }
    return result;
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Timeless DEV post...

How to write a kickass README

Arguably the single most important piece of documentation for any open source project is the README. A good README not only informs people what the project does and who it is for but also how they use and contribute to it.

If you write a README without sufficient explanation of what your project does or how people can use it then it pretty much defeats the purpose of being open source as other developers are less likely to engage with or contribute towards it.