Advertisement
Spocoman

05. Account Balance

Dec 27th, 2021 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function accountBalance(input) {
  2.     let index = 0, total = 0;
  3.     let command;
  4.  
  5.     while ((command = input[index++]) !== "NoMoreMoney") {
  6.         let cash = Number(command);
  7.         if (cash < 0) {
  8.             console.log("Invalid operation!");
  9.             break;
  10.         } else {
  11.             console.log(`Increase: ${cash.toFixed(2)}`);
  12.             total += cash;
  13.         }
  14.     }
  15.    
  16.     console.log(`Total: ${total.toFixed(2)}`)
  17. }
  18.  
  19. РЕШЕНИЕ С FOR:
  20.  
  21. function accountBalance(input) {
  22.     let total = 0;
  23.  
  24.     for (let i = 0; i < input.length && input[i] !== "NoMoreMoney"; i++) {
  25.         let cash = Number(input[i]);
  26.         if (cash < 0) {
  27.             console.log("Invalid operation!");
  28.             break;
  29.         } else {
  30.             console.log(`Increase: ${cash.toFixed(2)}`);
  31.             total += cash;
  32.         }
  33.     }
  34.  
  35.     console.log(`Total: ${total.toFixed(2)}`)
  36. }
  37.  
  38. ИЛИ С ТЕРНАРЕН ОПЕРАТОР ЛЕКО ТАРИКАТСКАТА:
  39.  
  40. function accountBalance(input) {
  41.     let total = 0;
  42.  
  43.     for (let i = 0; i < input.length && input[i] !== "NoMoreMoney"; i++) {
  44.         console.log(Number(input[i]) < 0 ? "Invalid operation!" : `Increase: ${Number(input[i]).toFixed(2)}`);
  45.         if (Number(input[i]) < 0) {
  46.             break;
  47.         }
  48.         total += Number(input[i]);
  49.     }
  50.    
  51.     console.log(`Total: ${total.toFixed(2)}`);
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement