Issue with Else If Statement

nparekh1979 - May 19 '21 - - Dev Community

Hi,

I am stuck trying to understand a Javascript solution. Please can you help dumb it down for me if possible:

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.

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.

FYI: I am a beginner

Thanks in advance

. . . . .
Terabox Video Player