使用三目運算代替簡單的if-else

能夠寫一行就不要寫三行,以下:javascript

let seatDiscount = 100;
if(seat < 5) {
    seatDiscount = 90;
} else if(seat < 10) {
    seatDiscount = 80;
} else {
    seatDiscount = 70;
}

能夠改爲三目運算符:java

t seatDiscount = seat < 5 ? 90 :
                           seat < 10 ? 80 : 70;
相關文章
相關標籤/搜索