Advertisement
TZinovieva

Coffee Lover JS Fundamentals Mid Exam

Feb 21st, 2023
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function coffeeLover(arr) {
  2.     let coffees = arr.shift().split(" ");
  3.     let numberOfCommands = Number(arr.shift());
  4.  
  5.     for (let i = 0; i < numberOfCommands; i++) {
  6.       let tokens = arr[i].split(" ");
  7.       let command = tokens[0];
  8.  
  9.       if (command === "Include") {
  10.         let coffee = tokens[1];
  11.         coffees.push(coffee);
  12.       } else if (command === "Remove") {
  13.         let direction = tokens[1];
  14.         let count = Number(tokens[2]);
  15.         if (direction === "first") {
  16.           coffees.splice(0, count);
  17.         } else if (direction === "last") {
  18.           coffees.splice(-count, count);
  19.         }
  20.       } else if (command === "Prefer") {
  21.         let index1 = Number(tokens[1]);
  22.         let index2 = Number(tokens[2]);
  23.         if (index1 >= 0 && index1 < coffees.length && index2 >= 0 && index2 < coffees.length) {
  24.           let temp = coffees[index1];
  25.           coffees[index1] = coffees[index2];
  26.           coffees[index2] = temp;
  27.         }
  28.       } else if (command === "Reverse") {
  29.         coffees.reverse();
  30.       }
  31.     }
  32.     console.log("Coffees:");
  33.     console.log(coffees.join(" "));
  34.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement