dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
2.7 KiB
2.7 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69b559d2903b9e4afe9075f7 | Challenge 237: Equation Validation | 28 | challenge-237 |
--description--
Given a string representing a math equation, determine whether it is correct.
- The left side may contain up to three positive integers and the operators
+,-,*, and/. - The equation will be given in the format:
"number operator number = number"(with two or three numbers on the left). For example:"2 + 2 = 4"or"2 + 3 - 1 = 4". - The right side will always be a single integer.
Follow standard order of operations: multiplication and division are evaluated before addition and subtraction, from left-to-right.
--hints--
isValidEquation("2 + 2 = 4") should return true.
assert.isTrue(isValidEquation("2 + 2 = 4"));
isValidEquation("2 + 3 - 1 = 4") should return true.
assert.isTrue(isValidEquation("2 + 3 - 1 = 4"));
isValidEquation("8 / 2 = 4") should return true.
assert.isTrue(isValidEquation("8 / 2 = 4"));
isValidEquation("10 * 5 = 50") should return true.
assert.isTrue(isValidEquation("10 * 5 = 50"));
isValidEquation("2 - 2 = 0") should return true.
assert.isTrue(isValidEquation("2 - 2 = 0"));
isValidEquation("2 + 9 / 3 = 5") should return true.
assert.isTrue(isValidEquation("2 + 9 / 3 = 5"));
isValidEquation("20 - 2 * 3 = 14") should return true.
assert.isTrue(isValidEquation("20 - 2 * 3 = 14"));
isValidEquation("2 + 5 = 6") should return false.
assert.isFalse(isValidEquation("2 + 5 = 6"));
isValidEquation("10 - 2 * 3 = 24") should return false.
assert.isFalse(isValidEquation("10 - 2 * 3 = 24"));
isValidEquation("3 + 9 / 3 = 4") should return false.
assert.isFalse(isValidEquation("3 + 9 / 3 = 4"));
--seed--
--seed-contents--
function isValidEquation(equation) {
return equation;
}
--solutions--
function isValidEquation(equation) {
const [left, right] = equation.split(" = ");
const tokens = left.split(" ");
for (let i = 1; i < tokens.length - 1; i += 2) {
const operator = tokens[i];
if (operator === "*" || operator === "/") {
const leftOperand = Number(tokens[i - 1]);
const rightOperand = Number(tokens[i + 1]);
const computed = operator === "*" ? leftOperand * rightOperand : leftOperand / rightOperand;
tokens.splice(i - 1, 3, String(computed));
i -= 2;
}
}
let result = Number(tokens[0]);
for (let i = 1; i < tokens.length - 1; i += 2) {
const operator = tokens[i];
const operand = Number(tokens[i + 1]);
result = operator === "+" ? result + operand : result - operand;
}
return result === Number(right);
}