編寫一個函數(shù) addWithSurcharge 將兩個金額與附加費相加。小于等于10的每一筆,附加費為1。大于10且小于等于20的每一筆,附加費為2。大于20的每一筆,附加費為3。調(diào)用addWithSurcharge( 10, 30) 應(yīng)該返回 44。我找到了答案,但我認(rèn)為它很長,我相信有一個簡短的答案 let cha = 1; let charge = 2; let surcharge = 3; function addWithSurcharge(x, y ){ let xe = xx(x); let ye = yy(y); let result = xe + ye console.log(result); } function xx(x){ if(x <= 10){ return x + cha } else if ( x > 10 && x <= 20){ return x + charge } else return x + surcharge ; } function yy(y){ if(y <= 10){ return y + cha } else if ( y > 10 && y <= 20){ return y + charge } else return y + surcharge ; } addWithSurcharge(1, 1 ) //4 addWithSurcharge(1, 20 ) //24 addWithSurcharge(10, 30 ) // 44
用 2 個值計算 3 個不同的金額
繁星淼淼
2022-07-21 20:50:58