TZinovieva

Name Game JS

Nov 29th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function nameGame(input) {
  2.     let index = 0;
  3.     let command = input[index];
  4.     index++;
  5.    
  6.     let bestPoints = 0;
  7.     let bestPlayer = "";
  8.     while (command !== "Stop") {
  9.  
  10.         let playerPoints = 0;
  11.         for (let i = 1; i <= command.length; i++){
  12.             let number = Number(input[index]);
  13.             index++;
  14.  
  15.             let letterValue = command.charCodeAt(i - 1);
  16.  
  17.                 if (number === letterValue) {
  18.                 playerPoints += 10;
  19.                 } else {
  20.                 playerPoints += 2;
  21.                 }
  22.         }
  23.         if (playerPoints >= bestPoints) {
  24.             bestPoints = playerPoints;
  25.             bestPlayer = command;
  26.         }
  27.         command = input[index];
  28.         index++;
  29.     }
  30.     console.log(`The winner is ${bestPlayer} with ${bestPoints} points!`);
  31. }
Advertisement
Add Comment
Please, Sign In to add comment