Advertisement
miroLLL

ATM Machine

Feb 9th, 2019
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.  
  3.     let bancnoted = [];
  4.  
  5.     for(let line of input){
  6.  
  7.         if(line.length > 2){
  8.  
  9.             let loadedMoney = line.reduce((a,b) => a + b);
  10.             Array.prototype.push.apply(bancnoted, line);
  11.  
  12.             bancnoted = bancnoted.sort((a,b) => b - a);
  13.  
  14.             console.log(`Service Report: ${loadedMoney}$ inserted. Current balance: ${bancnoted.reduce((a,b) => a+b)}$.`);
  15.  
  16.         } else if (line.length === 2){
  17.  
  18.             let currentAmount = line[0];
  19.             let wantsToWithdraw = line[1];
  20.  
  21.             if(currentAmount >= wantsToWithdraw){
  22.  
  23.                 if(bancnoted.length !== 0 && bancnoted.reduce((a,b) => a + b) >= wantsToWithdraw){
  24.  
  25.                         for(let i in bancnoted){
  26.  
  27.                             if(bancnoted[i] <= wantsToWithdraw){
  28.  
  29.                                wantsToWithdraw-= bancnoted[i];
  30.                                bancnoted.splice(i, 1, 0);
  31.                             }
  32.  
  33.                             if(wantsToWithdraw === 0){
  34.                                 console.log(`You get ${line[1]}$. Account balance: ${currentAmount - line[1]}$. Thank you!`)
  35.                                 break;
  36.                             }
  37.                         }
  38.  
  39.                         bancnoted = bancnoted.filter(b => b !== 0)
  40.  
  41.                 } else {
  42.                     console.log(`ATM machine is out of order!`);
  43.                 }
  44.  
  45.             } else {
  46.                 console.log(`Not enough money in your account. Account balance: ${currentAmount}$.`);
  47.             }
  48.  
  49.         } else {
  50.  
  51.             let currentBancnote = line[0];
  52.             let dublicates = bancnoted.filter(b => b === currentBancnote);
  53.             let times = 0;
  54.  
  55.             if(dublicates !== undefined){
  56.                 times += dublicates.length;
  57.             }
  58.  
  59.             console.log(`Service Report: Banknotes from ${currentBancnote}$: ${times}.`)
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement