Advertisement
ErolKZ

Untitled

Jun 30th, 2021
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1.  
  2. function solve (input) {
  3.  
  4. let wordNumValue = 0;
  5.  
  6. let player2 = 0;
  7.  
  8. let player1 = 0;
  9.  
  10. let nameOfPlayer2 = '';
  11.  
  12. let nameOfPlayer1 = '';
  13.  
  14.  
  15. for (let i = 0; i < input.length; i++) {
  16.  
  17. if (input[i] !== 'Stop') {
  18.  
  19. if (isNaN(input[i]) && input[i] !== 'Stop') {
  20.  
  21. player2 = 0;
  22. nameOfPlayer2 = '';
  23. nameOfPlayer2 = input[i];
  24.  
  25. for (let j = 0; j < input[i].length; j++) {
  26.  
  27.  
  28. wordNumValue = input[i].charCodeAt(j);
  29.  
  30. if (wordNumValue === Number(input[i + j + 1])) {
  31.  
  32. player2 += 10;
  33.  
  34.  
  35. } else {
  36.  
  37. player2 += 2;
  38.  
  39. }
  40.  
  41.  
  42.  
  43. } // Inner loop
  44.  
  45. }
  46.  
  47.  
  48. if (player2 > 0 && player1 === 0) {
  49.  
  50. player1 = player2;
  51.  
  52. nameOfPlayer1 = nameOfPlayer2;
  53.  
  54. }
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. } else {
  64.  
  65. break;
  66.  
  67. }
  68.  
  69.  
  70. } // Outer loop
  71.  
  72.  
  73.  
  74. if (player1 > player2) {
  75.  
  76. console.log(`The winner is ${nameOfPlayer1} with ${player1} points!`);
  77.  
  78. } else if (player2 > player1) {
  79.  
  80. console.log(`The winner is ${nameOfPlayer2} with ${player2} points!`);
  81.  
  82. } else {
  83.  
  84. console.log(`The winner is ${nameOfPlayer2} with ${player2} points!`);
  85.  
  86. }
  87.  
  88.  
  89. // console.log(nameOfPlayer1, player1, nameOfPlayer2, player2);
  90.  
  91.  
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement