Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function hungry(meals, commands) {
- let count = 0;
- for (let command = 0; command < commands.length; command++) {
- let firstPortion = meals[0];
- let served = meals[meals.length - 1];
- if (commands[command] === 'End') {
- break;
- }
- if (commands[command] === 'Serve') {
- if (meals.length > 0) {
- console.log(`${served} served!`);
- let currentIndex = meals.indexOf(served);
- if (currentIndex > -1) {
- meals.splice(currentIndex, 1);
- }
- }
- }
- if (commands[command] === 'Eat') {
- if (meals.length > 0) {
- console.log(`${firstPortion} eaten`);
- count++;
- let currentIndex = meals.indexOf(firstPortion);
- if (currentIndex > -1) {
- meals.splice(currentIndex, 1);
- }
- }
- }
- if (commands[command].split(/\s/)[0] === 'Add') {
- let newMeal = commands[command].split(/\s/)[1];
- if (newMeal !== '' && newMeal !== undefined) {
- meals.unshift(newMeal);
- }
- }
- if (commands[command].split(/\s/)[0] === 'Consume') {
- if (meals.length > 0) {
- let splicedArr = [];
- let startIndex = Number(commands[command].split(/\s/)[1]);
- let endIndex = Number(commands[command].split(/\s/)[2]);
- if (startIndex >= 0 && endIndex < meals.length) {
- console.log('Burp!');
- splicedArr = meals.splice(startIndex, endIndex - startIndex + 1);
- }
- count += splicedArr.length;
- }
- }
- if (commands[command].split(/\s/)[0] === 'Shift') {
- let startIndex = Number(commands[command].split(/\s/)[1]);
- let endIndex = Number(commands[command].split(/\s/)[2]);
- if (startIndex >= 0 && endIndex < meals.length) {
- let temp = meals[startIndex];
- meals[startIndex] = meals[endIndex];
- meals[endIndex] = temp;
- }
- }
- }
- if (meals.length !== 0) {
- console.log(`Meals left: ${meals.join(', ')}`);
- } else {
- console.log('The food is gone');
- }
- console.log(`Meals eaten: ${count}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement