Guest User

Untitled

a guest
Jun 4th, 2020
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //TODO
  2. function darts(input) {
  3.   let index = 0;
  4.   let player = input[index++];
  5.  
  6.   let points = 301;
  7.   let successfulShots = 0;
  8.   let unsuccessfulShots = 0;
  9.  
  10.   let command = input[index++];
  11.   while((command !== 'Retire') && points !== 0) {
  12.     let shot = Number(input[index++]);
  13.    
  14.     if(command === 'Double') {
  15.       shot *= 2;
  16.     } else if(command === 'Triple') {
  17.       shot *= 3;
  18.     }
  19.    
  20.     if(points >= shot) {
  21.       points -= shot;
  22.       successfulShots++;
  23.     } else {
  24.       unsuccessfulShots++;
  25.     }
  26.    
  27.     command = input[index++];
  28.   }
  29.  
  30.   if(points === 0) {
  31.     console.log(`${player} won the leg with ${successfulShots} shots.`);
  32.   } else if(command === 'Retire') {
  33.     console.log(`${player} retired after ${unsuccessfulShots} unsuccessful shots.`);
  34.   }
  35. }
Add Comment
Please, Sign In to add comment