MartenBG

Darts

Mar 11th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. function solve(input) {
  2. let name = input.shift();
  3. let sector = input.shift();
  4. let points = Number(input.shift());
  5. let leg = 301;
  6. let shots = 0;
  7. let isLeg = false;
  8. let fasleShots = 0;
  9.  
  10.  
  11. while (sector != 'Retire') {
  12. let currentPoints = 0;
  13. switch (sector) {
  14. case 'Triple': currentPoints += 3 * points;
  15. break;
  16. case 'Double': currentPoints += 2 * points;
  17. break;
  18. case 'Single': currentPoints += points;
  19. }
  20. if (leg - currentPoints >= 0) {
  21. shots++;
  22. leg -= currentPoints;
  23. } else {
  24. fasleShots++;
  25. }
  26. if (leg == 0) {
  27. isLeg = true;
  28. break;
  29. }
  30. sector = input.shift();
  31. points = Number(input.shift());
  32.  
  33. }
  34. if (isLeg) {
  35. console.log(`${name} won the leg with ${shots} shots.`)
  36. } else {
  37. console.log(`${name} retired after ${fasleShots} unsuccessful shots.`);
  38. }
  39. }
  40. //solve(['Michael van Gerwen', 'Triple', '20', 'Triple', '19', 'Double', '10', 'Single', '3', 'Single', '1', 'Triple', '20', 'Triple', '20', 'Double', '20']);
Advertisement
Add Comment
Please, Sign In to add comment