Advertisement
DimiPetrov

PB - Game Number Wars

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