Bank Terminal
Programming Basics · JavaScript
TASK
Problem
Given balance, operation `deposit`/`withdraw`, and amount. Deposit adds funds; withdrawal requires amount≤balance. Print new balance or `INSUFFICIENT FUNDS`.
EXAMPLE
Example
1000 withdraw 300
700
LIMITS
Constraints
All numeric input values fit in JavaScript `Number`.
LEARN
Theory for this problem
+
### Bank Terminal
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.', 'After each iteration, it should be clear what has already been accumulated and what remains to be processed.']
ANSWER
Solution
+
const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trimEnd();
let [b, op, a] = input.trim().split(/\s+/).filter(Boolean);
b = Number(b);
a = Number(a);
if ((op) === ("deposit")) {
console.log(b + a);
} else {
if (a <= b) {
console.log(b - a);
} else {
console.log("INSUFFICIENT FUNDS");
}
}