Advertisement
Spocoman

02. Treasure Hunt

Nov 10th, 2023
988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function treasureHunt(input) {
  2.     let chest = input.shift().split('|');
  3.  
  4.     while (true) {
  5.         tokens = input.shift().split(' ');
  6.         let command = tokens.shift();
  7.         if (command === "Yohoho!") {
  8.             break;
  9.         } else if (command === "Loot") {
  10.             for (let i = 0; i < tokens.length; i++) {
  11.                 if (!chest.includes(tokens[i])) {
  12.                     chest.unshift(tokens[i]);
  13.                 }
  14.             }
  15.         } else if (command === "Drop") {
  16.             let index = Number(tokens);
  17.             if (index >= 0 && index < chest.length) {
  18.                 chest.push(chest.splice(index, 1));
  19.             }
  20.         } else if (command === "Steal") {
  21.             let count = Number(tokens);
  22.             let stolenChest = chest.splice(-count);
  23.             console.log(stolenChest.join(", "));
  24.         }
  25.     }
  26.  
  27.     if (chest.length === 0) {
  28.         console.log("Failed treasure hunt.");
  29.     } else {
  30.         console.log(`Average treasure gain: ${((chest.join('').length) / chest.length).toFixed(2)} pirate credits.`);
  31.     }
  32.     return;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement