Advertisement
Guest User

darts

a guest
Feb 21st, 2020
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function darts(input) {
  2.    
  3.     let firstPoints = Number(input.shift());
  4.     let game = input.shift();
  5.    
  6.    
  7.    
  8.     let succsesfull = 0;
  9.     let unsuccsesfull = 0;
  10.  
  11.  
  12.     while (game !== "bullseye"){
  13.        
  14.         let points = Number(input.shift());
  15.         if (game == "number section") {
  16.             firstPoints -= points;
  17.             if (firstPoints >= 0){
  18.             succsesfull++;
  19.             } else {
  20.             unsuccsesfull++;
  21.             }
  22.         } else if (game == "double ring") {
  23.             firstPoints -= points *2;
  24.             if (firstPoints>=0) {
  25.                
  26.                 succsesfull++;
  27.             } else {
  28.                 unsuccsesfull++;
  29.             }
  30.         } else if (game == "triple ring") {
  31.             firstPoints -= points *3;
  32.             if (firstPoints>=0) {
  33.                 succsesfull++;
  34.             } else {
  35.                 unsuccsesfull++;
  36.             }
  37.         }
  38.         if (firstPoints == 0) {
  39.             console.log(`Congratulations! You won the game in ${succsesfull} moves!`);
  40.         break;
  41.         }
  42.      game = input.shift();
  43.  
  44.     }
  45.     if (game == "bullseye"){
  46.         console.log(`Congratulations!  You won the game with a bullseye in ${succsesfull} moves!`);
  47.  
  48.     } else {
  49.         console.log(`Sorry, you lost. Score difference: ${unsuccsesfull}.`);
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement