Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2023
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(grades) {
  2.     grades = grades.map(Number);
  3.     let studentsNumber = grades.shift();
  4.  
  5.     let sumAllGrades = 0;
  6.     let lowOrEq2_99 = 0;
  7.     let lowOrEq3_99 = 0;
  8.     let lowOrEq4_99 = 0;
  9.     let bigOrEq5 = 0;
  10.  
  11.     for (let grade of grades) {
  12.         if (grade < 2 || grade > 6) {
  13.             continue;
  14.         }
  15.         if (grade <= 2.99) {
  16.             lowOrEq2_99++;
  17.         } else if (grade <= 3.99) {
  18.             lowOrEq3_99++;
  19.         } else if (grade <= 4.99) {
  20.             lowOrEq4_99++;
  21.         } else {
  22.             bigOrEq5++;
  23.         }
  24.         sumAllGrades += grade;
  25.     }
  26.  
  27.     let averageGrade = (sumAllGrades / studentsNumber).toFixed(2);
  28.     let percentFailed = (lowOrEq2_99 / studentsNumber) * 100;
  29.     let percent3_99 = (lowOrEq3_99 / studentsNumber) * 100;
  30.     let percent4_99 = (lowOrEq4_99 / studentsNumber) * 100;
  31.     let percent5 = (bigOrEq5 / studentsNumber) * 100;
  32.  
  33.     console.log(`Top students: ${percent5.toFixed(2)}%`);
  34.     console.log(`Between 4.00 and 4.99: ${percent4_99.toFixed(2)}%`);
  35.     console.log(`Between 3.00 and 3.99: ${percent3_99.toFixed(2)}%`);
  36.     console.log(`Fail: ${percentFailed.toFixed(2)}%`);
  37.     console.log(`Average: ${averageGrade}`);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement