TZinovieva

Easter Eggs Battle JS

Dec 23rd, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function easterEggsBattle(input) {
  2.     let firstPlayerEggs = Number(input[0]);
  3.     let secondPlayerEggs = Number(input[1]);
  4.     let index = 2;
  5.     let command = input[index];
  6.     index++;
  7.  
  8.     while (command !== "End") {
  9.  
  10.         if (command === "one") {
  11.             secondPlayerEggs--;
  12.         } else {
  13.             firstPlayerEggs--;
  14.         }
  15.         if (firstPlayerEggs === 0) {
  16.             console.log(`Player one is out of eggs. Player two has ${secondPlayerEggs} eggs left.`);
  17.             break;
  18.         } else if (secondPlayerEggs === 0) {
  19.             console.log(`Player two is out of eggs. Player one has ${firstPlayerEggs} eggs left.`);
  20.             break;
  21.         }
  22.         command = input[index];
  23.         index++;
  24.     }
  25.     if (command === "End") {
  26.         console.log(`Player one has ${firstPlayerEggs} eggs left.`);
  27.         console.log(`Player two has ${secondPlayerEggs} eggs left.`);
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment