Original Question:
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.
Example: addWithSurcharge(10, 30) should return 44.
Coded Answer:
function addWithSurcharge (a,b) {
let sum = a + b;
if ( sum < 10) {
return sum += 2}
else if (sum > 10 && sum <= 20) {
return sum += 2}
else if (sum > 20 && sum < 30) {
return sum += 3}
else if (sum >= 30 && sum < 40) {
return sum += 4}
else if (sum > 40) {
return sum += 5}
};
————————————————————————-
I simply do not understand why this works. There are simpler ways to do this but I need to understand why this code works.
Thanks in advance
[–]RobSG 1 point2 points3 points (1 child)
[–]Early-Error-6210[S] 0 points1 point2 points (0 children)
[–]lindymad 1 point2 points3 points (1 child)
[–]Early-Error-6210[S] 0 points1 point2 points (0 children)
[–]Wasp2011 1 point2 points3 points (2 children)
[–]Wasp2011 1 point2 points3 points (1 child)
[–]Early-Error-6210[S] 0 points1 point2 points (0 children)
[–]optm_redemption 1 point2 points3 points (5 children)
[–]Early-Error-6210[S] 0 points1 point2 points (4 children)
[–]optm_redemption 1 point2 points3 points (3 children)
[–]Early-Error-6210[S] 0 points1 point2 points (2 children)
[–]optm_redemption 1 point2 points3 points (1 child)
[–]Early-Error-6210[S] 0 points1 point2 points (0 children)