Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function treasure(arr) {
- let chest = arr.shift().split("|");
- let command = arr.shift();
- let steal = [];
- let counter = 0;
- while (command != "Yohoho!") {
- let tokens = command.split(" ");
- let action = tokens.shift();
- if (action == "Loot") {
- let treasureToLoot = tokens.slice(0);
- for (let treasure of treasureToLoot) {
- if (!chest.includes(treasure)) {
- chest.unshift(treasure);
- }
- }
- } else if (action == "Drop") {
- let idx = Number(tokens[0]);
- if (idx >= 0 && idx < chest.length) {
- let theLoot = chest.splice(idx, 1);
- chest.push(theLoot);
- }
- } else if (action == "Steal") {
- let count = Number(tokens[0]);
- if (count > chest.length + 1) {
- steal = chest;
- chest = []; //point 3
- //console.log(steal); //point 3
- } else {
- steal = chest.splice(-count,);
- }
- console.log(steal.join(", ")); //point 1
- steal = []; //point 2
- }
- //console.log(steal.join(", ")); //point 1
- command = arr.shift();
- }
- if (chest.length == 0) {
- console.log("Failed treasure hunt.");
- } else {
- for (let el of chest) {
- for (let i = 0; i < el.length; i++) {
- counter++;
- }
- }
- console.log(`Average treasure gain: ${(counter / chest.length).toFixed(2)} pirate credits.`)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement