Advertisement
Spocoman

Favorite Movie

Feb 21st, 2022 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function favoriteMovie(input) {
  2.     let bestMovie = "";
  3.     let bestAsciiSum = 0;
  4.     let counter = 0;
  5.     let movie;
  6.  
  7.     while ((movie = input.shift()) !== "STOP" && ++counter !== 7) {
  8.         let asciiSum = 0;
  9.         for (let j = 0; j < movie.length; j++) {
  10.             asciiSum += movie[j].charCodeAt(0);
  11.             if (movie[j].charCodeAt(0) > 96 && movie[j].charCodeAt(0) < 123) {
  12.                 asciiSum -= movie.length * 2;
  13.             } else if (movie[j].charCodeAt(0) > 64 && movie[j].charCodeAt(0) < 91) {
  14.                 asciiSum -= movie.length;
  15.             }
  16.  
  17.             if (asciiSum > bestAsciiSum) {
  18.                 bestAsciiSum = asciiSum;
  19.                 bestMovie = movie;
  20.             }
  21.         }
  22.     }
  23.  
  24.     if (counter === 7) {
  25.         console.log("The limit is reached.");
  26.     }
  27.     console.log(`The best movie for you is ${bestMovie} with ${bestAsciiSum} ASCII sum.`);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement