Ivankooo1

Treasure Hunt

Nov 2nd, 2019
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function treasureHunt(arr){
  2.   let loot = arr.shift().split('|');
  3.    
  4.   for(let items of arr){
  5.  
  6.      if(items === 'Yohoho!'){
  7.        break;
  8.      }
  9.      let treasure = items.split(' ');
  10.      let command = treasure.shift();
  11.      
  12.      switch(command){
  13.        case "Loot":
  14.           for(let item of treasure){
  15.  
  16.             if(loot.indexOf(item)===-1){
  17.               loot.unshift(item);
  18.             }
  19.           }
  20.          ;break;
  21.        case 'Drop':
  22.           let ind = Number(treasure[0]);
  23.           if(ind < 0 || ind >=loot.length){
  24.             break;
  25.           }
  26.           let el = loot.splice(ind,1)[0];
  27.           loot.push(el);
  28.          ;break;
  29.        case 'Steal':
  30.           let count = Number(treasure[0]);
  31.           let ind2 = loot.length - count;
  32.           let steal = loot.splice(ind2,count);
  33.           console.log(steal.join(', '));
  34.          ;break;
  35.      }
  36.   }
  37.   if(loot.length === 0){
  38.     console.log('Failed treasure hunt.')
  39.   }
  40.     let sum = 0;
  41.     for(let i = 0;i < loot.length;i++){
  42.         sum += loot[i].length;
  43.     }
  44.     let averageGain = sum / loot.length;
  45.     console.log(`Average treasure gain: ${averageGain.toFixed(2)} pirate credits.`)
  46.  
  47.  
  48. }
  49. treasureHunt([
  50. 'Gold|Silver|Bronze|Medallion|Cup',
  51. 'Loot Wood Gold Coins',
  52. 'Loot Silver Pistol',
  53. 'Drop 3',
  54. 'Steal 3',
  55. 'Yohoho!'
  56. ])
Advertisement
Add Comment
Please, Sign In to add comment