TZinovieva

Favourite movie JS

Dec 2nd, 2022
57
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function favoriteMovie(input) {
  2.     let index = 0;
  3.     let name = input[index];
  4.     index++;
  5.  
  6.     let favouriteName = "";
  7.     let bestPoints = 0;
  8.     let counter = 0;
  9.     while (name !== "STOP") {
  10.         counter++;
  11.         let points = 0;
  12.         let movieValue = 0;
  13.         for (let i = 0; i < name.length; i++) {
  14.              points = name.charCodeAt(i);  
  15.        
  16.             if (points >= 97 && points <= 122) {
  17.                 points -= name.length * 2;
  18.             } else if (points >= 65 && points <= 90) {
  19.                 points -= name.length;
  20.             }
  21.             movieValue += points;
  22.         }
  23.             if (movieValue >= bestPoints) {
  24.                 bestPoints = movieValue;
  25.                 favouriteName = name;
  26.             }
  27.             name = input[index];
  28.             index++;
  29.  
  30.             if (counter === 7) {
  31.                 break;
  32.             }
  33.     }
  34.     if (counter >= 7) {
  35.         console.log(`The limit is reached.`);
  36.         console.log(`The best movie for you is ${favouriteName} with ${bestPoints} ASCII sum.`);
  37.     } else {
  38.         console.log(`The best movie for you is ${favouriteName} with ${bestPoints} ASCII sum.`)
  39.     }
  40. }
Advertisement
Comments
  • TZinovieva
    2 years
    # text 0.13 KB | 0 0
    1. The JavaScript string charCodeAt() method is used to find out the Unicode value of a character at the specific index in a string.
Add Comment
Please, Sign In to add comment