Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function accountBalance(input) {
- let index = 0, total = 0;
- let command;
- while ((command = input[index++]) !== "NoMoreMoney") {
- let cash = Number(command);
- if (cash < 0) {
- console.log("Invalid operation!");
- break;
- } else {
- console.log(`Increase: ${cash.toFixed(2)}`);
- total += cash;
- }
- }
- console.log(`Total: ${total.toFixed(2)}`)
- }
- РЕШЕНИЕ С FOR:
- function accountBalance(input) {
- let total = 0;
- for (let i = 0; i < input.length && input[i] !== "NoMoreMoney"; i++) {
- let cash = Number(input[i]);
- if (cash < 0) {
- console.log("Invalid operation!");
- break;
- } else {
- console.log(`Increase: ${cash.toFixed(2)}`);
- total += cash;
- }
- }
- console.log(`Total: ${total.toFixed(2)}`)
- }
- ИЛИ С ТЕРНАРЕН ОПЕРАТОР ЛЕКО ТАРИКАТСКАТА:
- function accountBalance(input) {
- let total = 0;
- for (let i = 0; i < input.length && input[i] !== "NoMoreMoney"; i++) {
- console.log(Number(input[i]) < 0 ? "Invalid operation!" : `Increase: ${Number(input[i]).toFixed(2)}`);
- if (Number(input[i]) < 0) {
- break;
- }
- total += Number(input[i]);
- }
- console.log(`Total: ${total.toFixed(2)}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement