Advertisement
Liliana797979

viarno reshenie darts

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