Advertisement
GalinaKG

Untitled

Mar 11th, 2022
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. function numberWars (input) {
  2. let index = 0;
  3. let nameOf1stPlayer = input[index];
  4. index++;
  5. let nameOf2ndPlayer = input[index];
  6. index++;
  7. let command = input[index];
  8. let result1st = 0;
  9. let result2nd = 0;
  10.  
  11. while(command !== "End of game") {
  12. let cardOf1st = Number(input[index]);
  13. index++;
  14. let cardOf2nd = Number(input[index]);
  15. index++;
  16.  
  17. if (cardOf1st > cardOf2nd) {
  18. result1st += cardOf1st - cardOf2nd;
  19.  
  20. } else if (cardOf2nd > cardOf1st) {
  21. result2nd += cardOf2nd - cardOf1st;
  22.  
  23. } else if (cardOf1st === cardOf2nd) {
  24. console.log("Number wars!");
  25. cardOf1st = Number(input[index]);
  26. index++;
  27. cardOf2nd = Number(input[index]);
  28.  
  29. if (cardOf1st > cardOf2nd) {
  30. console.log(`${nameOf1stPlayer} is winner with ${result1st} points`);
  31. } else if (cardOf2nd > cardOf1st) {
  32. console.log(`${nameOf2ndPlayer} is winner with ${result2nd} points`);
  33. }
  34. break;
  35. }
  36.  
  37. command = input[index];
  38. }
  39.  
  40. if (command === "End of game") {
  41. console.log(`${nameOf1stPlayer} has ${result1st} points`);
  42. console.log(`${nameOf2ndPlayer} has ${result2nd} points`);
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement