Liliana797979

my problem 2 - mid exam

Jul 22nd, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.      
  2. function problem2(array) {
  3.     let biscuits = array.shift().split(', ');
  4.     let command = array.shift();
  5.  
  6.     while (command != 'No More Money') {
  7.         let tokens = command.split(' ');
  8.         let action = tokens[0];
  9.  
  10.         switch (action) {
  11.             case 'OutOfStock':
  12.                 let outOfStockBiscuit = tokens[1];
  13.                 for (let i = 0; i < biscuits.length; i++) {
  14.                     let biscuit = biscuits[i];
  15.                     if (biscuit == outOfStockBiscuit) {
  16.                         biscuits.splice(i, 1, 'None')
  17.                     }
  18.                 }
  19.                 break;
  20.             case 'Required':
  21.                 let requiredBiscuit = tokens[1];
  22.                 let index = Number(tokens[2]);
  23.                 if (index >= 0 && index < biscuits.length) {
  24.                     if (biscuits[index] != 'None') {
  25.                         biscuits.splice(index, 1, requiredBiscuit);
  26.                     }
  27.                 }
  28.                 break;
  29.             case 'Last':
  30.                 let pushBiscuit = tokens[1];
  31.                 biscuits.push(pushBiscuit);
  32.                 break;
  33.         }
  34.  
  35.         command = array.shift();
  36.     }
  37.  
  38.     let biscuitsForPrint = biscuits.filter(x => x != 'None');
  39.     console.log(biscuitsForPrint.join(' '));
  40. }
Advertisement
Add Comment
Please, Sign In to add comment