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