dilyana2001

Untitled

Jul 9th, 2021 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function treasureHunt(arr) {
  2.     let chest = arr.shift().split('|');
  3.  
  4.     arr.forEach(row => {
  5.         let [command, ...args] = row.split(' ');
  6.         if (command === 'Yohoho!') {
  7.             if (chest.length !== 0) {
  8.                 let averageGain = 0;
  9.                 chest.forEach(item => averageGain += item.length);
  10.                 console.log(`Average treasure gain: ${(averageGain/chest.length).toFixed(2)} pirate credits.`);
  11.             } else console.log(`Failed treasure hunt.`);
  12.         } else if (command === 'Loot') loot(...args);
  13.         else if (command === 'Drop') drop(...args);
  14.         else if (command === 'Steal') steal(...args);
  15.     })
  16.  
  17.     function loot(...items) {
  18.         items.forEach(item => {
  19.             if (!chest.includes(item)) chest.unshift(item);
  20.         })
  21.     }
  22.  
  23.     function drop(index) {
  24.         if (index < chest.length && index > -1) {
  25.             let item = chest.splice(index, 1);
  26.             chest.push(item);
  27.         }
  28.     }
  29.  
  30.     function steal(count) {
  31.         if (count > chest.length) count = chest.length;
  32.         let stolen = chest.splice(chest.length - count, count);
  33.         console.log(stolen.join(', '));
  34.     }
  35. }
Add Comment
Please, Sign In to add comment