I solved this exercise on jshero.com but I know the solution can be written more cleanly, I just don't know how.
Here are the directions:
Write a function addWithSurcharge that adds two amounts with surcharge. For each amount less than or equal to 10, the surcharge is 1. For each amount greater than 10 and less than or equal to 20, the surcharge is 2. For each amount greater than 20, the surcharge is 3.
This challenge also supposes that you use if...else if...else to solve the exercise. The course also hasn't touched on functions within functions, so I'm mainly concerned with simplification and readability, but also curious about ternaries. Here is my attempt which works,
function addWithSurcharge(num1, num2) {
let surcharge = 0;
if (num1 <= 10) {
surcharge += 1;
} else if (num1 > 10 && num1 <= 20) {
surcharge += 2;
} else {
surcharge += 3;
}
if (num2 <= 10) {
surcharge += 1;
} else if (num2 > 10 && num2 <= 20) {
surcharge += 2;
} else {
surcharge += 3;
}
return surcharge + num1 + num2;
}
Thank you, much appreciated!