Advertisement
Spocoman

Game Number Wars

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