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)
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:
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 the44
you wanted.I'll format the function you listed here to make things easier to read):
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 to40
and can see where40
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 theif
conditions match. It'll returnundefined
(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
and11, 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.
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
This isn't correct. What about 15 and 4, for example? or 20 and 0?
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.
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 !!
I still am confused and my specific query is:
Can you simplify this for me.
Let's cut it down a bit. This bit here is redundant:
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:
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.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.