Advertisement
Guest User

Treasure hunt

a guest
Sep 16th, 2020
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. function treasureHunt(input) {
  2. let initialChest = input.shift().split("|");
  3. let commandLine = input.shift();
  4.  
  5. while (commandLine !== "Yohoho!") {
  6. if (commandLine.includes("Loot")) {
  7. let array = commandLine.split(" ");
  8. let command = array.shift();
  9. let loot = array.shift();
  10. while (loot !== undefined) {
  11. if (!initialChest.includes(loot)) {
  12. initialChest.unshift(loot);
  13. }
  14. loot = array.shift();
  15. }
  16. } else if (commandLine.includes("Drop")) {
  17. let [command, index] = commandLine.split(" ");
  18. index = Number(index);
  19. if (index > -1 && index < initialChest.length) {
  20. initialChest.push(initialChest[index]);
  21. initialChest.splice(index, 1);
  22. }
  23. } else if (commandLine.includes("Steal")) {
  24. let [command, count] = commandLine.split(" ");
  25. count = Number(count);
  26. if (count > initialChest.lenght) {
  27. console.log(initialChest.join(', '));
  28. initialChest = [];
  29. } else {
  30. let stolen = initialChest.splice(initialChest.length - count);
  31. console.log(stolen.join(", "));
  32. }
  33. }
  34. commandLine = input.shift();
  35. }
  36.  
  37. let lengthSum = 0;
  38. let count = 0;
  39. initialChest.forEach((element) => {
  40. lengthSum += element.length;
  41. count++;
  42. });
  43.  
  44. let averageGain = lengthSum / count;
  45.  
  46. if (initialChest.length === 0) {
  47. console.log(`Failed treasure hunt.`);
  48. } else {
  49. console.log(
  50. `Average treasure gain: ${averageGain.toFixed(2)} pirate credits.`
  51. );
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement