HackerRank Js-Week2 2. Grading Students

    HackerRank

    HackerLand University has the following grading policy:
    Each student has a grade between 0 and 100, inclusive.
    Any grade below 40 is a failing grade.
    Sam is a professor at a university and he likes to round each student’s grade according to the following rules:
    If the difference between the grade and the next multiple of 5 is less than 3, round to the next multiple of 5.
    If the grade value is less than 38, no rounding occurs because the result is still a failing grade.

    在 HackerLand 大學中有依據以下評分標準:
    
    每一個學生的成績在 0 ~ 100 之間。
    任何低於 40 分的成績都是不及格的。
    Sam 是大學的教授,他喜歡根據以下規則對每個學生的成績進行四捨五入:
    如果成績與下一個 5 的倍數之間差異小於 3,則四捨五入到下一個 5 的倍數。
    如果成績小於 38,則不會進行四捨五入,因為結果是不及格。
    

    Example 1:

    grades = [73, 67, 38, 33]
    

    solution:
    遍歷 grades 陣列中的元素,依據以下條件:
    – 如果分數小於 38,則 continue。
    – 如果分數除以 5 後的餘數為 3,則代表該分數 + 2 會剛好等於 5 的倍數。
    – 如果分數除以 5 後的餘數為 4,則代表該分數 + 1 會剛好等於 5 的倍數。
    回傳 grades 陣列。

    Code 1: BigO(n)

    '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) {
        // Write your code here
        for (let i = 0; i < grades.length; i++) {
            
            //grades < 38, return
            if (grades[i] < 38){
                continue;
            } else {
                let temp = grades[i]
                let remainder = grades[i] % 5
                
                //grades % 5 < 3, remove 5 multiple
                if (remainder === 3) {
                    temp = temp + 2
                    grades[i] = temp   
                } else if (remainder === 4) {
                    temp = temp + 1
                    grades[i] = temp
                }
            }
        }
        return grades;
    }
        
    
    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();
    }
    
    

    FlowChart:
    Example 1

    grades = [73, 67, 38, 33]
    
    73 % 5 = 3 //73 + 2 = 75
    67 % 5 = 2 //67
    38 //38
    33 //33
    
    return [75, 67, 40, 33];