Advertisement
Liliana797979

viarno reshenie Game number wars

Feb 3rd, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.      
  2. function solve(input){
  3.   let playerOne = input.shift();
  4.   let playerTwo = input.shift();
  5.  
  6.   let command = input.shift();
  7.  
  8.   let pointsPlayerOne = 0;
  9.   let pointsPlayerTwo = 0;
  10.  
  11.   while (command !== "End of game"){
  12.     let cardPlayerOne = +command;
  13.     let cardPlayerTwo = +input.shift();
  14.  
  15.     if(cardPlayerOne > cardPlayerTwo){
  16.       pointsPlayerOne += cardPlayerOne - cardPlayerTwo;
  17.     } else if(cardPlayerOne < cardPlayerTwo){
  18.       pointsPlayerTwo += cardPlayerTwo - cardPlayerOne;
  19.     } else {
  20.       console.log(`Number wars!`);
  21.  
  22.       cardPlayerOne = +input.shift();
  23.       cardPlayerTwo = +input.shift();
  24.  
  25.       if(cardPlayerOne > cardPlayerTwo){
  26.         console.log(`${playerOne} is winner with ${pointsPlayerOne} points`);
  27.         return;
  28.       } else if(cardPlayerOne < cardPlayerTwo) {
  29.         console.log(`${playerTwo} is winner with ${pointsPlayerTwo} points`);
  30.         return;
  31.       }
  32.     }
  33.  
  34.     command = input.shift();
  35.   }
  36.  
  37.   if(command === 'End of game') {
  38.     console.log(`${playerOne} has ${pointsPlayerOne} points`);
  39.     console.log(`${playerTwo} has ${pointsPlayerTwo} points`);
  40.   }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement