Advertisement
TZinovieva

Inventory JS Fundamentals

Feb 17th, 2023
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function inventory(arr) {
  2.     let items = arr.shift().split(', ');
  3.     arr.pop();
  4.  
  5.     for (let i = 0; i < arr.length; i++) {
  6.       let tokens = arr[i];
  7.       let commands = tokens.split(' - ');
  8.       let command = commands[0];
  9.       let item = commands[1].split(':');
  10.       let oldItem = item[0];
  11.       let newItem = item[1];
  12.  
  13.       if (command === "Collect") {
  14.         if (!(items.includes(oldItem))) {
  15.           items.push(oldItem);
  16.         }
  17.       } else if (command === "Drop") {
  18.         if ((items.includes(oldItem))) {
  19.           let index = items.indexOf(oldItem);
  20.           if (index !== -1) {
  21.             items.splice(index, 1);
  22.           }
  23.         }
  24.       } else if (command === "Combine Items") {
  25.         if (items.includes(oldItem)) {
  26.           let index = items.indexOf(oldItem);
  27.           if (index !== -1) {
  28.             items.splice(index + 1, 0, newItem)
  29.           }
  30.         }
  31.       } else if (command === "Renew") {
  32.         if (items.includes(oldItem)) {
  33.           let index = items.indexOf(oldItem);
  34.           if (index !== -1) {
  35.             items.splice(index, 1);
  36.             items.push(oldItem);
  37.           }
  38.         }
  39.       }
  40.     }
  41.     console.log(items.join(', '));
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement