[프로그래머스 문제풀이31]상호 평가 자바스크립트

매드갈릭레드망고·2021년 9월 28일
0
function solution(scores) {
    const averages=[];
    const students=scores.map(val=>[]);
    for(const score of scores){
        score.map((stu,stuNum)=>{     
            students[stuNum].push(stu);
        })
    }
    for(let i=0;i<students.length;i++){
        const max=Math.max(...students[i]);
        const min=Math.min(...students[i]);
        let flag=-1;
        if(students[i][i]===max)
            flag=0;
        if(students[i][i]===min)
            flag=1;
        switch(flag){
            case -1:
                break;
            case 0:
                 for(const score of students[i]){
                     if(score===max)
                         flag+=1;
                 }
                if(flag!=1)
                    {
                        break;
                    }
                else{
                     students[i].splice(i,1);
                }
                 break;
            case 1:
                 for(const score of students[i]){
                     if(score===min)
                         flag+=1;
                 }
                if(flag!=2)
                    {
                        break;
                    }
                else{
                     students[i].splice(i,1);
                }
                break;
        }
        
        averages.push(students[i].reduce((a,b)=>a+b)/students[i].length);
    }
    
    return setGrade(averages);
}
function setGrade(averages){
    let answer="";
    const standards=[
        [101,90],
        [90,80],
        [80,70],
        [70,50],
        [50,0]
        ]
    const grades=["A","B","C","D","F"];
    for(const average of averages){
        for(let i=0;i<standards.length;i++){
            
            if(average<standards[i][0]&&average>=standards[i][1]){
                answer+=grades[i];
            }
            
        }
    }
    
    return answer;
}

'유일한' 조심

0개의 댓글