Advertisement
zarkoto223

gameNumberWars

Feb 13th, 2024
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function wars(input) {
  2.  
  3.     let playerOne = input[0];
  4.     let playerTwo = input[1];
  5.  
  6.     let onePlScore = 0;
  7.     let twoPlScore = 0
  8.  
  9.     for (let i = 2; i < input.length; i += 2) {
  10.  
  11.         if (input[i] === 'End of game') {
  12.             console.log(`${playerOne} has ${onePlScore} points`)
  13.             console.log(`${playerTwo} has ${twoPlScore} points`)
  14.             return
  15.         }
  16.  
  17.         let oneCard = Number(input[i])
  18.         let twoCard = Number(input[i + 1])
  19.  
  20.  
  21.         if (oneCard > twoCard) {
  22.             onePlScore += oneCard - twoCard
  23.         } else if (twoCard > oneCard) {
  24.             twoPlScore += twoCard - oneCard
  25.         } else {
  26.             console.log("Number wars!");
  27.             break;
  28.         }
  29.     }
  30.     if (onePlScore > twoPlScore) {
  31.         console.log(`${playerOne} is winner with ${onePlScore} points`)
  32.  
  33.     } else {
  34.         console.log(`${playerTwo} is winner with ${twoPlScore} points`)
  35.     }
  36. }
  37.  
  38. wars([
  39.     'Desi',
  40.     'Niki',
  41.     '7',
  42.     '5',
  43.     '3',
  44.     '4',
  45.     '3',
  46.     '3',
  47.     '5',
  48.     '3'
  49.  
  50.  
  51. ])
  52.  
  53.  
  54. // wars(['Aleks',
  55. // 'Georgi',
  56. // '4',
  57. // '5',
  58. // '3',
  59. // '2',
  60. // '4',
  61. // '3',
  62. // '4',
  63. // '4',
  64. // '5',
  65. // '2'
  66. //     ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement