Created
October 20, 2025 15:22
-
-
Save tatsuyax25/5ee6681b05b15448d1c3fd3a719640d6 to your computer and use it in GitHub Desktop.
There is a programming language with only four operations and one variable X: ++X and X++ increments the value of the variable X by 1.
--X and X-- decrements the value of the variable X by 1.
Initially, the value of X is 0. Given an array of string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {string[]} operations | |
| * @return {number} | |
| */ | |
| var finalValueAfterOperations = function(operations) { | |
| // Step 1: Initialize the variable X to 0 | |
| let X = 0; | |
| // Step 2: Loop through each operation in the array | |
| for (let i = 0; i < operations.length; i++) { | |
| let op = operations[i]; | |
| // Step 3: Check if the operation is an increment | |
| if (op === "++X" || op === "X++") { | |
| X += 1; // Increase X by 1 | |
| } | |
| // Step 4: Check if the operation is a decrement | |
| else if (op === "--X" || op === "X--") { | |
| X -= 1; // Decrease X by 1 | |
| } | |
| // No need for an else cluase since all inputs are guaranteed to be valid | |
| } | |
| // Step 5: Return the final value of X | |
| return X; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment