DimiPetrov

PB- Darts Tournament

Jun 6th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.    let index = 0;
  3.    let points = Number(input[index]);
  4.    index++;
  5.    let isBullseye = false;
  6.    let counter = 0;
  7.  
  8.    while(points > 0) {
  9.        let sector = input[index];
  10.        index++;
  11.        let score = Number(input[index]);
  12.        index++;
  13.  
  14.        switch(sector) {
  15.            case 'number section': points -= score; break;
  16.            case 'double ring': points -= score * 2; break;
  17.            case 'triple ring': points -= score * 3; break;
  18.            case 'bullseye': isBullseye = true; break;
  19.        }
  20.        if(isBullseye === true) {
  21.            break;
  22.        }
  23.        counter++;
  24.    }
  25.      if(points === 0) {
  26.            console.log(`Congratulations! You won the game in ${counter} moves!`);
  27.      } else if(isBullseye) {
  28.            console.log(`Congratulations! You won the game with a bullseye in ${counter} moves!`);
  29.      } else {
  30.            console.log(`Sorry, you lost. Score difference: ${Math.abs(points)}.`);
  31.      }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment