Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(meals, args) {
- let portions = 0;
- const shiftPlates = (first, second, arr) => {
- const firstIndex = Number(first);
- const secondIndex = Number(second);
- if (arr[firstIndex] !== undefined && arr[secondIndex] !== undefined) {
- const firstMeal = arr[firstIndex];
- const secondMeal = arr[secondIndex];
- arr.splice(firstIndex, 1, secondMeal);
- arr.splice(secondIndex, 1, firstMeal);
- }
- };
- const consumeMeals = (first, second, arr) => {
- const firstIndex = Number(first);
- const secondIndex = Number(second);
- if (arr[firstIndex] !== undefined && arr[secondIndex] !== undefined) {
- const count = secondIndex - firstIndex + 1;
- arr.splice(firstIndex, count);
- console.log('Burp!');
- return count;
- }
- return 0;
- };
- const infoMealCommand = (command) => {
- switch (command[0]) {
- case 'Serve': {
- if (meals.length < 1) {
- break;
- }
- console.log(`${meals.pop()} served!`);
- break;
- }
- case 'Add':
- if (command[1] === undefined) break;
- meals.unshift(command[1]);
- break;
- case 'Shift':
- shiftPlates(command[1], command[2], meals);
- break;
- case 'Eat':
- if (meals.length < 1) {
- break;
- }
- console.log(`${meals.shift()} eaten`);
- portions++;
- break;
- case 'Consume':
- portions += consumeMeals(command[1], command[2], meals);
- break;
- default:
- }
- };
- for (let i = 0; i < args.length; i++) {
- const command = args[i].split(' ');
- if (command == 'End') {
- break;
- }
- infoMealCommand(command);
- }
- if (meals.length > 0) {
- console.log(`Meals left: ${meals.join(', ')}`);
- } else {
- console.log('The food is gone');
- }
- console.log(`Meals eaten: ${portions}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement