Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input){
- let bancnoted = [];
- for(let line of input){
- if(line.length > 2){
- let loadedMoney = line.reduce((a,b) => a + b);
- Array.prototype.push.apply(bancnoted, line);
- bancnoted = bancnoted.sort((a,b) => b - a);
- console.log(`Service Report: ${loadedMoney}$ inserted. Current balance: ${bancnoted.reduce((a,b) => a+b)}$.`);
- } else if (line.length === 2){
- let currentAmount = line[0];
- let wantsToWithdraw = line[1];
- if(currentAmount >= wantsToWithdraw){
- if(bancnoted.length !== 0 && bancnoted.reduce((a,b) => a + b) >= wantsToWithdraw){
- for(let i in bancnoted){
- if(bancnoted[i] <= wantsToWithdraw){
- wantsToWithdraw-= bancnoted[i];
- bancnoted.splice(i, 1, 0);
- }
- if(wantsToWithdraw === 0){
- console.log(`You get ${line[1]}$. Account balance: ${currentAmount - line[1]}$. Thank you!`)
- break;
- }
- }
- bancnoted = bancnoted.filter(b => b !== 0)
- } else {
- console.log(`ATM machine is out of order!`);
- }
- } else {
- console.log(`Not enough money in your account. Account balance: ${currentAmount}$.`);
- }
- } else {
- let currentBancnote = line[0];
- let dublicates = bancnoted.filter(b => b === currentBancnote);
- let times = 0;
- if(dublicates !== undefined){
- times += dublicates.length;
- }
- console.log(`Service Report: Banknotes from ${currentBancnote}$: ${times}.`)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement