Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function easterShop(input) {
- let totalEggs = Number(input[0]);
- let index = 1;
- let command = input[index];
- index++;
- let soldEggs = 0;
- while (command !== "Close") {
- let eggs = Number(input[index]);
- index++;
- if (command === "Buy" && eggs > totalEggs) {
- console.log(`Not enough eggs in store!`);
- console.log(`You can buy only ${totalEggs}.`);
- return;
- }
- if (command === "Buy") {
- totalEggs -= eggs;
- soldEggs += eggs;
- } else {
- totalEggs += eggs;
- }
- command = input[index];
- index++;
- }
- if (command === "Close") {
- console.log(`Store is closed!`);
- console.log(`${soldEggs} eggs sold.`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment