Advertisement
Spocoman

Trip Expenses

Oct 5th, 2023 (edited)
1,043
-1
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function tripExpenses(input) {
  2.     let days = Number(input[0]);
  3.     let command;
  4.     let dayCash = 0.0;
  5.     let inputIndex = 1;
  6.  
  7.     for (let i = 0; i < days; i++) {
  8.         let dayProducts = 0;
  9.         dayCash += 60;
  10.  
  11.         while (true) {
  12.             command = input[inputIndex++];
  13.             if (command == "Day over") {
  14.                 console.log(`Money left from today: ${dayCash.toFixed(2)}. You\'ve bought ${dayProducts} products.`);
  15.                break;
  16.            }
  17.  
  18.            let productPrice = Number(command);
  19.            if (productPrice <= dayCash) {
  20.                dayCash -= productPrice;
  21.                dayProducts++;
  22.            }
  23.  
  24.            if (dayCash == 0) {
  25.                console.log(`Daily limit exceeded! You\'ve bought ${dayProducts} products.`);
  26.                break;
  27.            }
  28.        }
  29.    }
  30.    return;
  31. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement