-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradingStudents.js
127 lines (95 loc) · 3.15 KB
/
gradingStudents.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
HackerLand University has the following grading policy:
Every student receives a in the inclusive range from to .
Any less than is a failing grade.
Sam is a professor at the university and likes to round each student's according to these rules:
If the difference between the and the next multiple of is less than , round up to the next multiple of .
If the value of is less than , no rounding occurs as the result will still be a failing grade.
Examples
round to (85 - 84 is less than 3)
do not round (result is less than 40)
do not round (60 - 57 is 3 or higher)
Given the initial value of for each of Sam's students, write code to automate the rounding process.
Function Description
Complete the function gradingStudents in the editor below.
gradingStudents has the following parameter(s):
int grades[n]: the grades before rounding
Returns
int[n]: the grades after rounding as appropriate
Input Format
The first line contains a single integer, , the number of students.
Each line of the subsequent lines contains a single integer, .
Constraints
Sample Input 0
4
73
67
38
33
Sample Output 0
75
67
40
33
Explanation 0
image
Student received a , and the next multiple of from is . Since , the student's grade is rounded to .
Student received a , and the next multiple of from is . Since , the grade will not be modified and the student's final grade is .
Student received a , and the next multiple of from is . Since , the student's grade will be rounded to .
Student received a grade below , so the grade will not be modified and the student's final grade is .
*/
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'gradingStudents' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts INTEGER_ARRAY grades as parameter.
*/
function gradingStudents(grades) {
let finalGrade = [];
let multipleOfFive = 0
let resultMinus = 0
for (let i = 0; i < grades.length; i++) {
if (grades[i] < 38) {
finalGrade.push(grades[i]);
}
else {
// Round up the grade to the nearest number multiple of 5
multipleOfFive = Math.ceil(grades[i] / 5) * 5;
resultMinus = multipleOfFive - grades[i];
if (resultMinus < 3) {
finalGrade.push(multipleOfFive);
} else {
finalGrade.push(grades[i]);
}
}
}
return finalGrade;
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const gradesCount = parseInt(readLine().trim(), 10);
let grades = [];
for (let i = 0; i < gradesCount; i++) {
const gradesItem = parseInt(readLine().trim(), 10);
grades.push(gradesItem);
}
const result = gradingStudents(grades);
ws.write(result.join('\n') + '\n');
ws.end();
}