Advertisement
Spocoman

04. Train The Trainers

Dec 30th, 2021 (edited)
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function trainTheTrainers(input) {
  2.     let scoreNum = Number(input[0]);
  3.     let presentation = input[1];
  4.     let presentationCounter = 0;
  5.     let totalAverage = 0;
  6.     let index = 2;
  7.    
  8.     while (presentation !== 'Finish'){
  9.         let grade = 0
  10.         for (let i = 0; i < scoreNum; i++){
  11.             grade += Number(input[index++]);
  12.         }
  13.         console.log(`${presentation} - ${(grade / scoreNum).toFixed(2)}.`);
  14.         totalAverage += grade / scoreNum;
  15.         presentation = input[index++];
  16.         presentationCounter++;
  17.     }
  18.     console.log(`Student\'s final assessment is ${(totalAverage / presentationCounter).toFixed(2)}.`);
  19. }
  20.  
  21. Решение със shift():
  22.  
  23. function trainTheTrainers(input) {
  24.    let scoreNum = Number(input.shift());
  25.    let presentation = input.shift();
  26.    let presentationCounter = 0;
  27.    let totalAverage = 0;
  28.  
  29.    while (presentation !== 'Finish'){
  30.        let grade = 0
  31.        for (let i = 0; i < scoreNum; i++){
  32.            grade += Number(input.shift());
  33.        }
  34.        console.log(`${presentation} - ${(grade / scoreNum).toFixed(2)}.`);
  35.        totalAverage += grade / scoreNum;
  36.        presentation = input.shift();
  37.        presentationCounter++;
  38.    }
  39.    console.log(`Student\'s final assessment is ${(totalAverage / presentationCounter).toFixed(2)}.`);
  40. }
  41.  
  42. РЕШЕНИЕ С FOR И SHIFT():
  43.  
  44. function trainTheTrainers(input) {
  45.    let scoreNum = Number(input.shift());
  46.    let totalAverage = 0;
  47.  
  48.    for (let i = 0; i < Number.MAX_SAFE_INTEGER; i++) {
  49.        let presentation = input.shift();
  50.        if (presentation === 'Finish') {
  51.            console.log(`Student\'s final assessment is ${(totalAverage / i).toFixed(2)}.`);
  52.            break;
  53.        }
  54.        let grade = 0;
  55.        for (let j = 0; j < scoreNum; j++) {
  56.            grade += Number(input.shift());
  57.        }
  58.        console.log(`${presentation} - ${(grade / scoreNum).toFixed(2)}.`);
  59.        totalAverage += grade / scoreNum;
  60.    }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement