DimiPetrov

PB - Darts

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