Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function treasureHunt(arr){
- let loot = arr.shift().split('|');
- for(let items of arr){
- if(items === 'Yohoho!'){
- break;
- }
- let treasure = items.split(' ');
- let command = treasure.shift();
- switch(command){
- case "Loot":
- for(let item of treasure){
- if(loot.indexOf(item)===-1){
- loot.unshift(item);
- }
- }
- ;break;
- case 'Drop':
- let ind = Number(treasure[0]);
- if(ind < 0 || ind >=loot.length){
- break;
- }
- let el = loot.splice(ind,1)[0];
- loot.push(el);
- ;break;
- case 'Steal':
- let count = Number(treasure[0]);
- let ind2 = loot.length - count;
- let steal = loot.splice(ind2,count);
- console.log(steal.join(', '));
- ;break;
- }
- }
- if(loot.length === 0){
- console.log('Failed treasure hunt.')
- }
- let sum = 0;
- for(let i = 0;i < loot.length;i++){
- sum += loot[i].length;
- }
- let averageGain = sum / loot.length;
- console.log(`Average treasure gain: ${averageGain.toFixed(2)} pirate credits.`)
- }
- treasureHunt([
- 'Gold|Silver|Bronze|Medallion|Cup',
- 'Loot Wood Gold Coins',
- 'Loot Silver Pistol',
- 'Drop 3',
- 'Steal 3',
- 'Yohoho!'
- ])
Advertisement
Add Comment
Please, Sign In to add comment