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