if 条件判断和三元表达式的应用

此题来源为codewars上的题目
if 条件判断和三元表达式的应用
我的解答一:使用if进行的判断

function points(games) {
    var points=0
    for(var i = 0;i<games.length;i++){
        console.log(games[i])
        if(games[i].split(':')[0]>games[i].split(':')[1]){
        points+=3
    }
else if(games[i].split(':')[0]<games[i].split(':')[1]){
        points+=0
        }
else{
        points+=1}
        }
        return points
}
console.log(points(["1:1","2:0","3:0","4:0","2:1","3:1","4:1","3:2","4:2","4:3"]))

我的解答二:使用三元表达式的解决方案

function points(games) {
    var points = 0
    for (var i = 0; i < games.length; i++) {
        games[i].split(':')[0] > games[i].split(':')[1] ? points += 3 : games[i].split(':')[0] < games[i].split(':')[1] ?  points += 0 :points += 1
}
return points
}
console.log(points(["1:1", "2:0", "3:0", "4:0", "2:1", "3:1", "4:1", "3:2", "4:2", "4:3"]))

别人的解答一:使用 reduce 函数进行的叠加
if 条件判断和三元表达式的应用

const points = games => games.reduce((output, current) => {
    return output += current[0] > current[2] ? 3 : current[0] === current[2] ? 1 : 0;}, 0)
console.log(points(["1:1", "2:0", "3:0", "4:0", "2:1", "3:1", "4:1", "3:2", "4:2", "4:3"]))

关于reduce函数,摘抄了 https://www.cnblogs.com/ZengYunChun/p/9684957.html