5kov

Problem 1 – Cafeteria

Dec 17th, 2023 (edited)
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function cafeteria(array){
  2.     let baristasArray = [];
  3.  
  4.     for (let index = 1; index <= parseInt(array[0]); index++) {
  5.         let barista = {};
  6.         barista['name'] = array[index].split(' ')[0];
  7.         barista['shift'] = array[index].split(' ')[1];
  8.         barista['drinks'] = array[index].split(' ')[2].split(',');
  9.         baristasArray.push(barista);
  10.     }
  11.  
  12.     for (let index = parseInt(array[0]) + 1; index < array.length; index++) {
  13.         let commandArray = array[index].split(' / ');
  14.        
  15.         if (commandArray[0] === 'Closed') {
  16.             break;
  17.         } else if (commandArray[0] === 'Prepare') {
  18.             for (let index = 0; index < baristasArray.length; index++) {
  19.                 if (baristasArray[index].name === commandArray[1]) {
  20.                     if (baristasArray[index].shift === commandArray[2] && baristasArray[index].drinks.includes(commandArray[3])) {
  21.                         console.log(baristasArray[index].name + ' has prepared a ' + commandArray[3] + ' for you!')
  22.                     } else {
  23.                         console.log(baristasArray[index].name + ' is not available to prepare a ' + commandArray[3] + '.')
  24.                     }
  25.                 }
  26.             }
  27.         } else if (commandArray[0] === 'Change Shift') {
  28.             for (let index = 0; index < baristasArray.length; index++) {
  29.                 if (baristasArray[index].name === commandArray[1]) {
  30.  
  31.                     baristasArray[index].shift = commandArray[2];
  32.                     console.log(baristasArray[index].name + ' has updated his shift to: ' + commandArray[2])
  33.                 }    
  34.             }
  35.  
  36.         } else if (commandArray[0] === 'Learn') {
  37.             for (let index = 0; index < baristasArray.length; index++) {
  38.                 if (baristasArray[index].name === commandArray[1]) {
  39.                     if (baristasArray[index].drinks.includes(commandArray[2])) {
  40.                         console.log(baristasArray[index].name + ' knows how to make ' + commandArray[2] + '.');
  41.                     } else {
  42.                         baristasArray[index].drinks.push(commandArray[2]);
  43.                         console.log(baristasArray[index].name + ' has learned a new coffee type: ' + commandArray[2] + '.');
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.     }
  49.  
  50.     for (let index = 0; index < baristasArray.length; index++) {
  51.         let name = baristasArray[index].name;
  52.         let shift = baristasArray[index].shift;
  53.         let drinks = baristasArray[index].drinks.join(', ');
  54.         console.log('Barista: ' + name + ', Shift: ' + shift + ', Drinks: ' + drinks);        
  55.     }
  56. }
  57.  
  58. cafeteria([
  59.     '3',
  60.       'Alice day Espresso,Cappuccino',
  61.       'Bob night Latte,Mocha',
  62.       'Carol day Americano,Mocha',
  63.       'Prepare / Alice / day / Espresso',
  64.       'Change Shift / Bob / night',
  65.       'Learn / Carol / Latte',
  66.       'Learn / Bob / Latte',
  67.       'Prepare / Bob / night / Latte',
  68.       'Closed']
  69.     );
Advertisement
Add Comment
Please, Sign In to add comment