Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function easterShop(input) {
- let availableEggs = Number(input[0]);
- let soldEggs = 0;
- let index = 1;
- let command = input[index++];
- while (command !== "Close") {
- let eggs = Number(input[index++]);
- if (command === "Buy") {
- if (availableEggs - eggs < 0) {
- break;
- }
- availableEggs -= eggs;
- soldEggs += eggs;
- } else if (command === "Fill") {
- availableEggs += eggs;
- }
- command = input[index++];
- }
- if (command === "Close") {
- console.log("Store is closed!");
- console.log(`${soldEggs} eggs sold.`);
- } else {
- console.log("Not enough eggs in store!");
- console.log(`You can buy only ${availableEggs}.`);
- }
- }
- Решение със shift:
- function easterShop(input) {
- let availableEggs = Number(input.shift());
- let soldEggs = 0;
- let command = input.shift();
- while (command !== "Close") {
- let eggs = Number(input.shift());
- if (command === "Buy") {
- if (availableEggs - eggs < 0) {
- break;
- }
- availableEggs -= eggs;
- soldEggs += eggs;
- } else if (command === "Fill") {
- availableEggs += eggs;
- }
- command = input.shift();
- }
- if (command === "Close") {
- console.log("Store is closed!");
- console.log(`${soldEggs} eggs sold.`);
- } else {
- console.log("Not enough eggs in store!");
- console.log(`You can buy only ${availableEggs}.`);
- }
- }
Add Comment
Please, Sign In to add comment