js if ... else ... 优化

594 阅读1分钟
// 当if ... else ... 过多时, 可以使用表驱动编程, 这样代码更简洁
    function caluComputer (score) {
        const table = {
            100: 'A',
            90: 'A',
            80: 'B',
            70: 'C',
            60: 'D',
            others: 'E'
        }
        return table[Math.floor(score/10)*10] || table['others'] 
}