Advertisement
Spocoman

02. Exam Preparation

Dec 28th, 2021 (edited)
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function examPreparation(input) {
  2.     let poorGradesLimit = Number(input[0]);
  3.     let poorGradesCount = 0;
  4.     let countProblems = 0;
  5.     let lastProblem = 0;
  6.     let sumGrades = 0;
  7.     let index = 1;
  8.    
  9.     let command = input[index];
  10.     while (command !== "Enough") {
  11.         lastProblem = command;
  12.         let grade = Number(input[index + 1]);
  13.  
  14.         if (grade <= 4) {
  15.             poorGradesCount++;
  16.             if (poorGradesCount === poorGradesLimit) {
  17.                 break;
  18.             }
  19.         }
  20.         index += 2;
  21.         countProblems++;
  22.         sumGrades += grade;
  23.         command = input[index];
  24.     }
  25.  
  26.     let averageGrade = sumGrades / countProblems;
  27.    
  28.     if (command !== "Enough") {
  29.         console.log(`You need a break, ${poorGradesCount} poor grades.`);
  30.     } else {
  31.         console.log(`Average score: ${averageGrade.toFixed(2)}\nNumber of problems: ${countProblems}\nLast problem: ${lastProblem}`);
  32.     }
  33. }
  34.  
  35. ИЛИ:
  36.  
  37. function examPreparation(input) {
  38.     let poorGradesLimit = Number(input[0]);
  39.     let poorGradesCount = 0;
  40.     let countProblems = 0;
  41.     let lastProblem = 0;
  42.     let sumGrades = 0;
  43.     let index = 1;
  44.  
  45.     while (true) {
  46.         let command = input[index++];
  47.         if (command === "Enough") {
  48.             console.log(`Average score: ${(sumGrades / countProblems).toFixed(2)}\nNumber of problems: ${countProblems}\nLast problem: ${lastProblem}`);
  49.             break;
  50.         }
  51.  
  52.         lastProblem = command;
  53.         let grade = Number(input[index++]);
  54.  
  55.         if (grade <= 4) {
  56.             poorGradesCount++;
  57.  
  58.             if (poorGradesLimit === poorGradesCount) {
  59.                 console.log(`You need a break, ${poorGradesCount} poor grades.`);
  60.                 break;
  61.             }
  62.         }
  63.         sumGrades += grade;
  64.         countProblems++;
  65.     }
  66. }
  67.  
  68.  
  69. РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР И ЛЕКО ТАРИКАТСКАТА:)
  70.  
  71. function examPreparation(input) {
  72.     let poorGradesCount = 0;
  73.     let sumGrade = 0;
  74.     let index = 1;
  75.    
  76.     while (input[index] !== "Enough" && poorGradesCount !== Number(input[0])) {
  77.         poorGradesCount += Number(input[++index]) <= 4 ? 1 : 0;
  78.         sumGrade += Number(input[index++]);
  79.     }
  80.  
  81.     console.log(input[index] !== "Enough" ? `You need a break, ${input[0]} poor grades.` :
  82.         `Average score: ${(sumGrade / parseInt(index / 2)).toFixed(2)}\nNumber of problems: ${parseInt(index / 2)}\nLast problem: ${input[index - 2]}`);
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement