Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function treasureHunt(line) {
- let treasure = line
- .shift()
- .split('|');
- line.forEach(tr => {
- let current = tr.split(' ');
- let command = current.shift();
- if (command === 'Yohoho!') {
- return;
- }
- if (command === 'Loot') {
- for (let i = 0; i < current.length; i++) {
- if (!treasure.includes(current[i])) {
- treasure.unshift(current[i]);
- }
- }
- }
- if (command === 'Drop') {
- let index = Number(current[0]);
- let moved = treasure[index];
- if (index >= 0) {
- treasure.splice(index, 1);
- treasure.push(moved);
- }
- }
- if (command === 'Steal') {
- let stolenItems = '';
- let index = Number(current[0]);
- if (index < treasure.length) {
- stolenItems = treasure.splice(treasure.length - index, index);
- console.log(stolenItems.join(', '));
- } else {
- stolenItems = treasure.splice(0).join(', ');
- console.log(stolenItems);
- }
- }
- });
- let numberOfLoots = treasure.length;
- let points = treasure
- .join('')
- .length;
- if (numberOfLoots > 0) {
- console.log(`Average treasure gain: ${(points / numberOfLoots).toFixed(2)} pirate credits.`);
- } else {
- console.log("Failed treasure hunt.");
- }
- }
- treasureHunt([
- 'Diamonds|Silver|Shotgun|Gold',
- 'Loot Silver Medals Coal',
- 'Drop -1',
- 'Drop 1',
- 'Steal 12',
- 'Yohoho!'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment