DEV Community

Cover image for Issue with Else If Statement
nparekh1979
nparekh1979

Posted on

Issue with Else If Statement

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

Top comments (6)

Collapse
 
moopet profile image
Ben Sinclair

I simply do not understand why this works.

Try it out in your head or on a piece of paper.

This is a teaching question, and I think it's prompting you to make another function for surcharges, like this:

function getSurcharge(amount) {
  if (amount) > 20) {
    return 3;
  }

  if (amount) > 10) {
    return 2;
  }

  return 1;
}

function addWithSurcharge (a,b) {
  return a + getSurcharge(a) + b + getSurcharge(b);
}
Enter fullscreen mode Exit fullscreen mode

That's clearer what's going on, right? You can walk through it with random numbers like 10 and 30 and see it returns 10 + 1 + 30 + 3 which is the 44 you wanted.

I'll format the function you listed here to make things easier to read):

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
  }
}
Enter fullscreen mode Exit fullscreen mode

This function is doing the same thing, and the way to figure it out is to mentally (or pen-and paperly) walk through it with numbers that are at the extreme edges of any bounds. What do I mean by that?

Well, we know that your provided example parameters, 10, 30 will add up to 40 and can see where 40 goes in your function... and it doesn't go anywhere. The last two conditions are checking whether the sum is less than 40 or is greater than 40 and so none of the if conditions match. It'll return undefined (I think).

If we take the target sum to be in the range 20-30 (picked at random) and walk through the function with numbers that should add up to that, we can try it with 0, 20 and 11, 9. You'll see, by playing around, that you can't choose two numbers over 10 that'll add up to anything below 20. If one's over 10, the other has to be under.
And that's what the example you gave is using for its logic. It's difficult to read partly because of the fact that it's testing against values which aren't the same as the ones given in the function arguments, so while working through it you have to keep mentally jumping back up to the top of the function.

Collapse
 
nparekh1979 profile image
nparekh1979

Thanks Ben.

So this is how I dumbed it down:

Step by Step Explanation:

we declare a function addWithSurcharge

it has two parameters

we create if / else if / else statements to execute a block of code

we declare a variable sum

we initialize it with a value

this value will be the sum of the two parameters which are numbers

we use if else statements through out to check the total value of the sum

if the sum of 2 digits is less than 10, then we can assume that both the digits are below 10

hence we can add a surcharge of 1 per digit which will make it 1+1 = 2

if the sum of 2 digits is above 10, but less than or equal to 20, then we can safely assume that both the digits will be less than or equal to 10, but not above 10

hence we can again add a surcharge of 1 per digit which will make it 1+1 = 2

if the sum of 2 digits is greater than 20, but less than 30, then we can safely assume that one digit will be less than or equal to 10, and the other digit will be less than or equal to 20

hence we can add a surcharge of 1 + 2 = 3

if the sum of 2 digits is greater than or equal to 30, but is less than 40, then we can safely assume that one digit will be less than or equal to 10, and the other digit will be more than 20

hence we can add a surcharge of 1 + 3 = 4

if the sum of 2 digits is greater than or equal to 30, but is less than 40, then we can also assume that both digits would be less than 20 or one digit could be less than 20 and the other could be equal to 20

hence we can add a surcharge of 2 + 2 = 4

if the sum is greater than 40 then we can assume that one digit will be above 20, and the other digit will be equal to 20

hence we can add a surcharge of 3 + 2 = 5

there is no else statement but we assume that in case both the digits are above 20 then the function will automatically add the surcharge for above 20

hence it will add a surcharge of 3 + 3 = 6

Collapse
 
moopet profile image
Ben Sinclair

if the sum of 2 digits is above 10, but less than or equal to 20, then we can safely assume that both the digits will be less than or equal to 10, but not above 10

This isn't correct. What about 15 and 4, for example? or 20 and 0?

if the sum of 2 digits is greater than 20, but less than 30, then we can safely assume that one digit will be less than or equal to 10, and the other digit will be less than or equal to 20

Try 15 and 14?

I think you've spent a while making these assumptions and then focused on implementing them without trying numbers out. That's what I mean by working it out with a pencil, and by trying the biggest or smallest numbers that will fit to see what happens.

Thread Thread
 
nparekh1979 profile image
nparekh1979

Hey Ben,

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 !!

Collapse
 
nparekh1979 profile image
nparekh1979

I still am confused and 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.

Can you simplify this for me.

Collapse
 
moopet profile image
Ben Sinclair

Let's cut it down a bit. This bit here is redundant:

  if ( sum < 10) {
    return sum += 2
  }
  else if (sum > 10 && sum <= 20) {
    return sum += 2
  }
Enter fullscreen mode Exit fullscreen mode

Because the first condition is always going to match if the second does, and they return the same thing.

If I cut out the stuff that's unnecessary but keep the same general structure, we can get to this:

function addWithSurcharge (a, b) {
  let sum = a + b;

  if (sum <= 20) {
    return sum += 2
  }

  if (sum <= 30) {
    return sum += 3
  }

  if (sum <= 40) {
    return sum += 4
  }

  return sum += 5
}
Enter fullscreen mode Exit fullscreen mode

I've taken out the redundant first condition and removed the last elseif because if the number isn't 40 or under, then by definition it has to be over 40.

I cannot understand this at all and am surprised as to how the code has passed the test.

Me too, it fails in a bunch of ways, most obviously at numbers which equal 40 or at the logic being "numbers up to and including 20" and then "numbers above and including 30" which are different. If the test tried numbers which matched those boundaries (which a test should do) then it'd fail. I've fixed that for the purposes of simplifying it, but it does mean that my code will give different results.

There's nothing in the requirements for surcharges over 3, but the example code goes up to 5. It's not clear whether this is supposed to be a surcharge of 10% of the multiple of ten below the number or what. That's the problem with requirements!

If we were to assume the pattern continued infinitely, we could probably do surcharge = Math.floor((amount - 1) / 10) + 1 and leave it at that.