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

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Image of Checkly

4 Playwright Locators Explained: Which One Should You Use?

- locator('.cta'): Fast but brittle
- getByText('Click me'): User-facing, but can miss broken accessibility
- getByRole('button', { name: 'Click me' }): Most robust, best for a11y
- getByTestId('cta-button'): Stable, but ignores UX

Watch video

👋 Kindness is contagious

DEV is better (more customized, reading settings like dark mode etc) when you're signed in!

Okay