Advertisement
Spocoman

04. Grades

Dec 25th, 2021 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function grades(input) {
  2.     let student = Number(input[0]);
  3.     let grade3 = 0;
  4.     let grade4 = 0;
  5.     let grade5 = 0;
  6.     let grade6 = 0;
  7.     let gradeSum = 0;
  8.  
  9.     for (let i = 1; i <= student; i++) {
  10.         let grade = Number(input[i]);
  11.  
  12.         if (grade >= 5) {
  13.             grade6++;
  14.         } else if (grade >= 4) {
  15.             grade5++;
  16.         } else if (grade >= 3) {
  17.             grade4++;
  18.         } else if (grade >= 2) {
  19.             grade3++;
  20.         }
  21.         gradeSum += grade;
  22.     }
  23.  
  24.     console.log(`Top students: ${(grade6 / student * 100).toFixed(2)}%`);
  25.     console.log(`Between 4.00 and 4.99: ${(grade5 / student * 100).toFixed(2)}%`);
  26.     console.log(`Between 3.00 and 3.99: ${(grade4 / student * 100).toFixed(2)}%`);
  27.     console.log(`Fail: ${(grade3 / student * 100).toFixed(2)}%`);
  28.     console.log(`Average: ${(gradeSum / student).toFixed(2)}`);
  29.  
  30. }
  31.  
  32. РЕШЕНИЕ С WHILE И SHIFT():
  33.  
  34. function grades(input) {
  35.     let student = Number(input.shift());
  36.     let grade3 = 0;
  37.     let grade4 = 0;
  38.     let grade5 = 0;
  39.     let grade6 = 0;
  40.     let gradeSum = 0;
  41.  
  42.     while (input.length !== 0) {
  43.         let grade = Number(input.shift());
  44.  
  45.         if (grade >= 5) {
  46.             grade6++;
  47.         } else if (grade >= 4) {
  48.             grade5++;
  49.         } else if (grade >= 3) {
  50.             grade4++;
  51.         } else if (grade >= 2) {
  52.             grade3++;
  53.         }
  54.         gradeSum += grade;
  55.     }
  56.  
  57.     console.log(`Top students: ${(grade6 / student * 100).toFixed(2)}%`);
  58.     console.log(`Between 4.00 and 4.99: ${(grade5 / student * 100).toFixed(2)}%`);
  59.     console.log(`Between 3.00 and 3.99: ${(grade4 / student * 100).toFixed(2)}%`);
  60.     console.log(`Fail: ${(grade3 / student * 100).toFixed(2)}%`);
  61.     console.log(`Average: ${(gradeSum / student).toFixed(2)}`);
  62.  
  63. }
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement