Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function treasureHunt(array) {
- let chest = array.shift().split("|");
- let commands = array.slice();
- let stolen = [];
- for (let i = 0; i < array.length; i++) {
- let current = commands.shift();
- if (current === "Yohoho!") {
- break;
- }
- let curr = current.split(" ");
- let comm = curr.shift();
- if (comm === "Loot") {
- for (let i = 0; i < curr.length; i++) {
- let item = curr[i];
- if (!chest.includes(item)) {
- chest.unshift(item);
- }
- }
- } else if (comm === "Drop") {
- let index = curr.shift();
- let item = chest.splice(+index, 1);
- chest.push(item);
- } else if (comm === "Steal") {
- let count = Number(curr.shift());
- stolen = chest.slice(-count);
- }
- }
- console.log(stolen.join(", "));
- if (chest.length != stolen.length) {
- let sum = 0;
- let counter = 0;
- for (let i = 0; i < chest.length - stolen.length; i++) {
- sum += chest[i].length;
- counter++;
- }
- let averageGain = sum / counter;
- console.log(
- `Average treasure gain: ${averageGain.toFixed(2)} pirate credits.`
- );
- } else {
- console.log("Failed treasure hunt.");
- }
- }
- treasureHunt([
- "Gold|Silver|Bronze|Medallion|Cup",
- "Loot Wood Gold Coins",
- "Loot Silver Pistol",
- "Drop 3",
- "Steal 3",
- "Yohoho!",
- ]);
- treasureHunt([
- "Diamonds|Silver|Shotgun|Gold",
- "Loot Silver Medals Coal",
- "Drop -1",
- "Drop 1",
- "Steal 6",
- "Yohoho!",
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement