Didart

Train The Trainers - Nested Loops

Apr 19th, 2022
891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function trainTheTrainers(input) {
  2.    
  3.     let n = Number(input[0]);
  4.     let index = 1;
  5.     let command = input[index];
  6.  
  7.     let sumGrades = 0;
  8.     let counter = 0;
  9.  
  10.     while (command !== 'Finish') {
  11.         counter++;
  12.         let name = command;
  13.         let tempSumGrades = 0;
  14.  
  15.         for (let i = 0; i < n; i++) {
  16.             index++;
  17.             let grade = Number(input[index]);
  18.             tempSumGrades += grade;
  19.         }
  20.         let tempAvgGrade = tempSumGrades / n;
  21.         sumGrades += tempAvgGrade;
  22.  
  23.         console.log(`${name} - ${tempAvgGrade.toFixed(2)}.`);
  24.  
  25.         index++;
  26.         command = input[index];
  27.     }
  28.     let avgGrade = sumGrades / counter;
  29.  
  30.     console.log(`Student's final assessment is ${avgGrade.toFixed(2)}.`);
  31.  
  32. }
  33.  
  34. trainTheTrainers(["2", "While-Loop", "6.00", "5.50", "For-Loop", "5.84", "5.66", "Finish"])
  35.  
  36.  
  37.  
Advertisement
Add Comment
Please, Sign In to add comment