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
I've been a professional C, Perl, PHP and Python developer.
I'm an ex-sysadmin from the late 20th century.
These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
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.
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 !!