Click here to Skip to main content
15,038,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am stuck trying to understand a Javascript solution.

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.

I simply do not understand why this works.

My specific query is:

The condition that if the sum is greater than 10 and less than equal to 20 should have a surcharge to be added of 2 is also confusing. For this condition the surcharge to be added according to me can even be 3

I am a bit confused with the statement: else if (sum > 40) { return sum += 5}. I understand that if the sum is above 40 then there is a good chance that both the digits are above 20 and hence the surcharge should be 6 instead of 5. I cannot understand this at all and am surprised as to how the code has passed the test.

I am also confused as to why is there no else statement in the end, and why do I need to assume that the else statement will automatically identify the numbers above 20 and add 6 as a surcharge.

Please can you help dumb it down for me if possible. Thank you in advance.

What I have tried:

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}

}
Posted
Updated 19-May-21 0:32am
Comments
Richard MacCutchan 19-May-21 6:39am
   
Read the question again. There are only three surcharge values:
If the number is less than 10 - surcharge = 1
Else If the number is less than 20 - surcharge = 2
Else - surcharge = 3 : because the amount must be greater than 20

So you need to check both values and add the surcharge for each.
Nerav Parekh 27-May-21 13:37pm
   
Hey Richard,

Hope all is well !!

To check for yourself, I'd urge you to visit this website:

JSHero.net

Check question 49 - else if exercise

You can check the code and see for yourself.

I am sure I have made some error in understanding this but you may also have a look at it yourself.

Let me know what you find.

Stay Safe !!
Richard MacCutchan 28-May-21 3:07am
   
Why would I need to? You have shown the question in your post above. If what you posted is not correct then please change it.

1 solution

Not your question, but:
If sum equals 10, there is no match in your code.
JavaScript
if ( sum < 10) {

return sum += 2}

else if (sum > 10 && sum <= 20) { // when you reach this line, you already know the sum is not < 10

return sum += 2}

which allow
JavaScript
function addWithSurcharge (a,b) {

let sum = a + b;

if ( sum < 10) {

return sum += 2}

else if (sum <= 20) { // which include 10

return sum += 2}

else if (sum < 30) {
   
Comments
Nerav Parekh 27-May-21 13:38pm
   
Hey Patrice,

Hope all is well !!

To check for yourself, I'd urge you to visit this website:

JSHero.net

Check question 49 - else if exercise

You can check the code and see for yourself.

I am sure I have made some error in understanding this but you may also have a look at it yourself.

Let me know what you find.

Stay Safe !!
Patrice T 27-May-21 13:47pm
   
After 40 years of programming, I am pretty sure of what I said.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900