Advertisement
Guest User

Untitled

a guest
May 21st, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input = []) {
  2.     let initialLoot = input.shift().split('|');
  3.     let command = input.shift();
  4.  
  5.     while (command !== 'Yohoho!') {
  6.         let currCommand = command.split(' ');
  7.  
  8.         if (currCommand[0] === 'Loot') {
  9.  
  10.             for (let i = 1; i < currCommand.length; i++) {
  11.                 let el = currCommand[i];
  12.  
  13.                 if (initialLoot.indexOf(el) === -1) {
  14.                     initialLoot.unshift(el);
  15.                 }
  16.             }
  17.         } else if (currCommand[0] === 'Drop') {
  18.             let index = +currCommand[1];
  19.  
  20.             if (index >= 0 && index <= initialLoot.length) {
  21.                 let lastEl = initialLoot[index];
  22.                 initialLoot.splice(index, 1);
  23.                 initialLoot.push(lastEl);
  24.             }
  25.         } else if (currCommand[0] === 'Steal') {
  26.             let count = +currCommand[1];
  27.             let stolenList = [];
  28.             if (count > initialLoot.length) {
  29.                 for (let i = 0; i < initialLoot.length; i++) {
  30.                     stolenList.push(initialLoot[i]);
  31.                 }
  32.                 console.log(stolenList.reverse().join(', '));
  33.  
  34.             } else {
  35.                 let stolenList = [];
  36.                 let count = +currCommand[1];
  37.  
  38.                 for (let i = 1; i <= count; i++) {
  39.                     let lastEl = initialLoot.pop()
  40.                     stolenList.push(lastEl)
  41.                 }
  42.                 console.log(stolenList.reverse().join(', '));
  43.             }
  44.  
  45.         }
  46.         command = input.shift()
  47.     }
  48.  
  49.     if (initialLoot.length === 0) {
  50.         console.log(`Failed treasure hunt.`);
  51.     }
  52.  
  53.     else {
  54.         let sum = ' ';
  55.         let sum2 = 0
  56.  
  57.         for (let i = 0; i < initialLoot.length; i++) {
  58.             sum += initialLoot[i]
  59.             sum2 = sum.length - 1;
  60.         }
  61.  
  62.         console.log(`Average treasure gain: ${(sum2 / 5).toFixed(2)} pirate credits.`);
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement