kstoyanov

03. Inventory Programming Fundamentals Mid Exam

Jul 4th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function exam(args) {
  2.   const materials = args.shift().split(', ');
  3.  
  4.   const isFind = (matArr, material) => {
  5.     const isMaterialFind = matArr.find((mat) => mat === material);
  6.  
  7.     return isMaterialFind;
  8.   };
  9.  
  10.   const findPosition = (matArr, material) => {
  11.     const index = matArr.indexOf(material);
  12.     return index;
  13.   };
  14.  
  15.   args.forEach((elelement) => {
  16.     const [command, material] = elelement.split(' - ');
  17.     const isFound = isFind(materials, material);
  18.  
  19.  
  20.     switch (command) {
  21.       case 'Collect':
  22.  
  23.  
  24.         if (isFound === undefined) {
  25.           materials.push(material);
  26.         }
  27.         break;
  28.       case 'Drop':
  29.         if (isFound !== undefined) {
  30.           materials.splice(findPosition(materials, material), 1);
  31.         }
  32.         break;
  33.       case 'Combine Items':
  34.         const [oldItem, newItem] = material.split(':');
  35.         const findOldEx = isFind(materials, oldItem);
  36.         if (findOldEx !== undefined) {
  37.           const oldItemIndex = findPosition(materials, oldItem);
  38.           materials.splice(oldItemIndex + 1, 0, newItem);
  39.         }
  40.         break;
  41.       case 'Craft!':
  42.         console.log(materials.join(', '));
  43.         break;
  44.  
  45.       case 'Renew':
  46.         if (isFound !== undefined) {
  47.           materials.splice(findPosition(materials, material), 1);
  48.  
  49.           materials.push(material);
  50.         }
  51.         break;
  52.     }
  53.   });
  54.  
  55. }
Add Comment
Please, Sign In to add comment