Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function examPreparation(input) {
- let poorGradesLimit = Number(input[0]);
- let poorGradesCount = 0;
- let countProblems = 0;
- let lastProblem = 0;
- let sumGrades = 0;
- let index = 1;
- let command = input[index];
- while (command !== "Enough") {
- lastProblem = command;
- let grade = Number(input[index + 1]);
- if (grade <= 4) {
- poorGradesCount++;
- if (poorGradesCount === poorGradesLimit) {
- break;
- }
- }
- index += 2;
- countProblems++;
- sumGrades += grade;
- command = input[index];
- }
- let averageGrade = sumGrades / countProblems;
- if (command !== "Enough") {
- console.log(`You need a break, ${poorGradesCount} poor grades.`);
- } else {
- console.log(`Average score: ${averageGrade.toFixed(2)}\nNumber of problems: ${countProblems}\nLast problem: ${lastProblem}`);
- }
- }
- ИЛИ:
- function examPreparation(input) {
- let poorGradesLimit = Number(input[0]);
- let poorGradesCount = 0;
- let countProblems = 0;
- let lastProblem = 0;
- let sumGrades = 0;
- let index = 1;
- while (true) {
- let command = input[index++];
- if (command === "Enough") {
- console.log(`Average score: ${(sumGrades / countProblems).toFixed(2)}\nNumber of problems: ${countProblems}\nLast problem: ${lastProblem}`);
- break;
- }
- lastProblem = command;
- let grade = Number(input[index++]);
- if (grade <= 4) {
- poorGradesCount++;
- if (poorGradesLimit === poorGradesCount) {
- console.log(`You need a break, ${poorGradesCount} poor grades.`);
- break;
- }
- }
- sumGrades += grade;
- countProblems++;
- }
- }
- РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР И ЛЕКО ТАРИКАТСКАТА:)
- function examPreparation(input) {
- let poorGradesCount = 0;
- let sumGrade = 0;
- let index = 1;
- while (input[index] !== "Enough" && poorGradesCount !== Number(input[0])) {
- poorGradesCount += Number(input[++index]) <= 4 ? 1 : 0;
- sumGrade += Number(input[index++]);
- }
- console.log(input[index] !== "Enough" ? `You need a break, ${input[0]} poor grades.` :
- `Average score: ${(sumGrade / parseInt(index / 2)).toFixed(2)}\nNumber of problems: ${parseInt(index / 2)}\nLast problem: ${input[index - 2]}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement