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.
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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.