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