-Enigmos-

movieRating.js

Oct 25th, 2021 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function movieRating(input) {
  2.     let index = 0;
  3.     let moviesNum = Number(input[index]);
  4.     index++;
  5.     let topMovie;
  6.     let worstMovie;
  7.     let lowestScore = 0;
  8.     let highestScore = 0;
  9.     let sumRating = 0;
  10.  
  11.     for (i = 0; i < moviesNum; i++) {
  12.         let currentMovie = input[index];
  13.         index++;
  14.         let currentScore = Number(input[index]);
  15.         index++;
  16.         sumRating += currentScore;
  17.  
  18.         if (currentScore > highestScore) {
  19.             currentScore = highestScore;
  20.             topMovie = currentMovie;
  21.         }
  22.        
  23.         lowestScore = currentScore;
  24.         worstMovie = currentMovie;
  25.         if (currentScore < lowestScore) {
  26.             lowestScore = currentScore;
  27.         }
  28.     }
  29.     console.log(`${topMovie} is with highest rating: ${highestScore}`);
  30.     console.log(`${worstMovie} is with lowest rating: ${lowestScore}`);
  31.     console.log(`Average rating: ${(sumRating / moviesNum).toFixed(1)}`);
  32. }
  33.  
  34. movieRating(["5", "A Star is Born", "7.8", "Creed 2", "7.3", "Mary Poppins", "7.2", "Vice", "7.2", "Captain Marvel", "7.1"]);
  35. movieRating(["3", "Interstellar", "8.5", "Dangal", "8.3", "Green Book", "8.2"]);
Add Comment
Please, Sign In to add comment