Advertisement
VeselaVideva

Mid Exam - 07.11.2020 - 02. Crafting

Nov 8th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function crafting(input) {
  2.     let weapon = input.shift().split('|');
  3.     let outputEven = [];
  4.     let outputOdd = [];
  5.  
  6.     for (let command of input) {
  7.         if (command === 'Done' || command === undefined) {
  8.             break;
  9.         } else {
  10.             command = command.split(' ');
  11.  
  12.             if (command[1] === 'Left') {
  13.                 let index = Number(command[2]);
  14.                 if (index >= 0 && index < weapon.length) { // if the index exist
  15.                     let index1 = index;
  16.                     let index2 = index - 1;
  17.                     if (index2 >= 0 && index2 < weapon.length) { // if the move is possible
  18.                         let temp = weapon[index1];
  19.                         weapon.splice(index1, 1, weapon[index2]);
  20.                         weapon.splice(index2, 1, temp);
  21.                     }
  22.                 }
  23.  
  24.             } else if (command[1] === 'Right') {
  25.                 let index = Number(command[2]);
  26.                 if (index >= 0 && index < weapon.length) { // if the index exist
  27.                     let index1 = index;
  28.                     let index2 = index + 1;
  29.                     if (index2 >= 0 && index2 < weapon.length) { // if the move is possible
  30.                         let temp = weapon[index1];
  31.                         weapon.splice(index1, 1, weapon[index2]);
  32.                         weapon.splice(index2, 1, temp);
  33.                     }
  34.                 }
  35.  
  36.             } else if (command[0] === 'Check') {
  37.                 if (command[1] === 'Even') {
  38.                     for (let i = 0; i < weapon.length; i++) {
  39.                         if (i % 2 === 0) {
  40.                             outputEven.push(weapon[i]);
  41.                         }
  42.                     }
  43.                 }
  44.                 if (command[1] === 'Odd') {
  45.                     for (let i = 0; i < weapon.length; i++) {
  46.                         if (i % 2 !== 0) {
  47.                             outputOdd.push(weapon[i]);
  48.                         }
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.     }
  54.  
  55.     if (outputEven.length > 0) {
  56.         console.log(outputEven.join(' '));
  57.     }
  58.     if (outputOdd.length > 0) {
  59.         console.log(outputOdd.join(' '));
  60.     }
  61.     console.log(`You crafted ${weapon.join('')}!`);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement