Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input = []) {
- let initialLoot = input.shift().split('|');
- let command = input.shift();
- while (command !== 'Yohoho!') {
- let currCommand = command.split(' ');
- if (currCommand[0] === 'Loot') {
- for (let i = 1; i < currCommand.length; i++) {
- let el = currCommand[i];
- if (initialLoot.indexOf(el) === -1) {
- initialLoot.unshift(el);
- }
- }
- } else if (currCommand[0] === 'Drop') {
- let index = +currCommand[1];
- if (index >= 0 && index <= initialLoot.length) {
- let lastEl = initialLoot[index];
- initialLoot.splice(index, 1);
- initialLoot.push(lastEl);
- }
- } else if (currCommand[0] === 'Steal') {
- let count = +currCommand[1];
- let stolenList = [];
- if (count > initialLoot.length) {
- for (let i = 0; i < initialLoot.length; i++) {
- stolenList.push(initialLoot[i]);
- }
- console.log(stolenList.reverse().join(', '));
- } else {
- let stolenList = [];
- let count = +currCommand[1];
- for (let i = 1; i <= count; i++) {
- let lastEl = initialLoot.pop()
- stolenList.push(lastEl)
- }
- console.log(stolenList.reverse().join(', '));
- }
- }
- command = input.shift()
- }
- if (initialLoot.length === 0) {
- console.log(`Failed treasure hunt.`);
- }
- else {
- let sum = ' ';
- let sum2 = 0
- for (let i = 0; i < initialLoot.length; i++) {
- sum += initialLoot[i]
- sum2 = sum.length - 1;
- }
- console.log(`Average treasure gain: ${(sum2 / 5).toFixed(2)} pirate credits.`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement