DEV Community

loading...

Discussion on: Daily Challenge #68 - Grade Book

Collapse
cgty_ky profile image
Cagatay Kaya

Too much ternary operators...

const grade = (a,b,c) =>{
    const mean = (a + b + c) / 3;

    const prefix = mean % 10 < 5 ? '-': 
                   mean % 10 > 5 ? '+': '';

    return   mean < 59 ?          `F (${mean})`:
             mean < 70 ? `D${prefix} (${mean})` : 
             mean < 80 ? `C${prefix} (${mean})`: 
             mean < 90 ? `B${prefix} (${mean})` : 
                         `A${prefix} (${mean})`;

}
Collapse
peledzohar profile image
Zohar Peled

I've Started to write a c# answer only to find out I'm going the exact same route as your answer...

Collapse
aminnairi profile image
Amin • Edited

You could use something like that:

({"-1": "-", "1": "+", "0": ""})[mean.toString().localeCompare("5")]

Not tested but I believe it would work in JavaScript.