Spocoman

Movie Ratings

Feb 25th, 2022 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function movieRatings(input) {
  2.     let index = 0;
  3.     let movieVolume = Number(input[index++]);
  4.     let maxRating = 0;
  5.     let minRating = 10;
  6.     let maxMovie = "";
  7.     let minMovie = "";
  8.     let average = 0;
  9.  
  10.     for (let i = 0; i < movieVolume; i++) {
  11.         let movie = input[index++];
  12.         let rating = Number(input[index++]);
  13.         if (rating > maxRating) {
  14.             maxMovie = movie;
  15.             maxRating = rating;
  16.         } else if (rating < minRating) {
  17.             minMovie = movie;
  18.             minRating = rating;
  19.         }
  20.         average += rating / movieVolume;
  21.     }
  22.     console.log(`${maxMovie} is with highest rating: ${maxRating.toFixed(1)}`);
  23.     console.log(`${minMovie} is with lowest rating: ${minRating.toFixed(1)}`);
  24.     console.log(`Average rating: ${average.toFixed(1)}`);
  25. }
  26.  
  27.  
  28. РЕШЕНИЕ СЪС SHIFT():
  29.  
  30. function movieRatings(input) {
  31.     let movieVolume = Number(input.shift());
  32.     let maxRating = 0;
  33.     let minRating = 10;
  34.     let maxMovie = "";
  35.     let minMovie = "";
  36.     let average = 0;
  37.  
  38.     for (let i = 0; i < movieVolume; i++) {
  39.         let movie = input.shift();
  40.         let rating = Number(input.shift());
  41.         if (rating > maxRating) {
  42.             maxMovie = movie;
  43.             maxRating = rating;
  44.         } else if (rating < minRating) {
  45.             minMovie = movie;
  46.             minRating = rating;
  47.         }
  48.         average += rating / movieVolume;
  49.     }
  50.     console.log(`${maxMovie} is with highest rating: ${maxRating.toFixed(1)}`);
  51.     console.log(`${minMovie} is with lowest rating: ${minRating.toFixed(1)}`);
  52.     console.log(`Average rating: ${average.toFixed(1)}`);
  53. }
Add Comment
Please, Sign In to add comment