PROBLEM 96
Hard

Smart Calculator

Programming Basics · JavaScript

</>
STATUS Not solved

TASK

Problem

#96

Given `a op b`, where op is `+ - * / // %`. For `/`, `//`, `%` with b=0 print `ERROR`; otherwise compute.

EXAMPLE

Example

Input
10 // 3
Output
3

LIMITS

Constraints

op is valid
📖
LEARN Theory for this problem
+

### Smart Calculator

JavaScript arithmetic works with numeric values stored in variables. A clear solution reads the needed values, computes the formula with `+`, `-`, `*`, `/` or `**`, and prints only the final result.

💡
NEED HELP? Hints
+

['Write the formula using named intermediate values if it contains more than one operation.', 'Check the empty/invalid fallback explicitly instead of relying on `undefined` or `NaN`. For “Smart Calculator” in “Programming Basics”, verify that the exact output format matches the statement.']

ANSWER Solution
+
const fs = require('fs');

let [a, operator, b] = fs.readFileSync(0, 'utf8').trim().split(/\s+/);
a = Number(a);
b = Number(b);

if (b === 0 && ['/', '//', '%'].includes(operator)) {
    console.log('ERROR');
} else if (operator === '+') {
    console.log(a + b);
} else if (operator === '-') {
    console.log(a - b);
} else if (operator === '*') {
    console.log(a * b);
} else if (operator === '/') {
    console.log(a / b);
} else if (operator === '//') {
    console.log(Math.floor(a / b));
} else {
    console.log(((a % b) + b) % b);
}