Advertisement
Guest User

Untitled

a guest
Oct 28th, 2023
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function treasure(arr) {
  2.   let chest = arr.shift().split("|");
  3.   let command = arr.shift();
  4.   let steal = [];
  5.   let counter = 0;
  6.   while (command != "Yohoho!") {
  7.     let tokens = command.split(" ");
  8.     let action = tokens.shift();
  9.     if (action == "Loot") {
  10.       let treasureToLoot = tokens.slice(0);
  11.       for (let treasure of treasureToLoot) {
  12.         if (!chest.includes(treasure)) {
  13.           chest.unshift(treasure);
  14.         }
  15.       }
  16.     } else if (action == "Drop") {
  17.       let idx = Number(tokens[0]);
  18.       if (idx >= 0 && idx < chest.length) {
  19.         let theLoot = chest.splice(idx, 1);
  20.         chest.push(theLoot);
  21.       }
  22.     } else if (action == "Steal") {
  23.       let count = Number(tokens[0]);
  24.       if (count > chest.length + 1) {
  25.         steal = chest;
  26.         chest = [];                             //point 3
  27.         //console.log(steal);                   //point 3
  28.       } else {
  29.         steal = chest.splice(-count,);
  30.       }
  31.       console.log(steal.join(", "));            //point 1
  32.       steal = [];                               //point 2
  33.     }
  34.     //console.log(steal.join(", "));            //point 1
  35.     command = arr.shift();
  36.   }
  37.   if (chest.length == 0) {
  38.     console.log("Failed treasure hunt.");
  39.   } else {
  40.     for (let el of chest) {
  41.       for (let i = 0; i < el.length; i++) {
  42.         counter++;
  43.       }
  44.     }
  45.     console.log(`Average treasure gain: ${(counter / chest.length).toFixed(2)} pirate credits.`)
  46.   }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement