Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function treasureHunt(array) {
- let initialLootArray = (array[0]).split("|");
- let index = 1;
- let currentCommand = array[index];
- let stealArray = [];
- while (currentCommand !== "Yohoho!") {
- let currentInput = currentCommand.split(" ");
- switch (currentInput[0]) {
- case "Loot":
- let lootIndex = 1;
- while (lootIndex < Number(currentInput.length)) {
- let currentItem = currentInput[lootIndex];
- if (!initialLootArray.includes(currentItem)) {
- initialLootArray.unshift(currentItem);
- }
- lootIndex++;
- } break;
- case "Drop":
- let dropIndex = Number(currentInput[1]);
- if (dropIndex >= 0 && dropIndex < Number(initialLootArray.length)) {
- let removedItem = initialLootArray.splice(dropIndex, 1);
- initialLootArray.push(removedItem[0]);
- } break;
- case "Steal":
- let numberOfStolenItems = Number(currentInput[1]);
- let stolenItems = initialLootArray.splice(-numberOfStolenItems);
- console.log(stolenItems.join(", "));
- break;
- }
- index++;
- currentCommand = array[index];
- }
- let averageTreasureGain = 0;
- for (let finalIndex = 0; finalIndex < Number(initialLootArray.length); finalIndex++) {
- let currentItemLength = Number((initialLootArray[finalIndex]).length);
- averageTreasureGain += currentItemLength;
- }
- if (averageTreasureGain > 0) {
- averageTreasureGain = averageTreasureGain / Number(initialLootArray.length);
- console.log(`Average treasure gain: ${averageTreasureGain.toFixed(2)} pirate credits.`);
- } else if (averageTreasureGain === 0) {
- console.log(`Failed treasure hunt.`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement