Advertisement
-Enigmos-

gameOfnames.js

Dec 14th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function gameOfnames(input) {
  2.     let index = 0;
  3.     let command = input[index];
  4.     index++;
  5.     let winner;
  6.     let highestPoints = Number.MIN_SAFE_INTEGER;
  7.  
  8.     while (command !== "Stop") {
  9.         let totalPoints = 0;
  10.  
  11.         for (let i = 0; i < command.length; i++) {
  12.             let currentNumber = Number(input[index]);
  13.             index++;
  14.             let letter = command[i];
  15.             let asciiLetter = String.fromCharCode(currentNumber);
  16.  
  17.             if (asciiLetter === letter) {
  18.                 totalPoints += 10;
  19.             } else {
  20.                 totalPoints += 2;
  21.             }
  22.         }
  23.  
  24.         if (totalPoints >= highestPoints) {
  25.             highestPoints = totalPoints;
  26.             winner = command;
  27.         }
  28.        
  29.         command = input[index];
  30.         index++;
  31.     }
  32.     console.log(`The winner is ${winner} with ${highestPoints} points!`);
  33. }
  34.  
  35. gameOfnames (["Ivan", "73", "20", "98", "110", "Ivo", "80", "65", "87", "Stop"]);
  36. gameOfnames (["Pesho", "124", "34", "111", "97", "99", "Gosho", "98", "124", "88", "76", "18", "Stop"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement