Welcome to SoftwareEngineering.SE. Since you're new here, you may be interested by reading the Open letter to students with homework problems first.
back to the problem, here are some hints to get you on the right track.
Removing the condition
Take one of the blocks:
if($total_magazines <= 30 && $total_magazines >= 21)
{
$price = 1.75;
$total_amount = $total_magazines * $price;
}
Inverting the order of conditions, one obtains (the code means nearly the same thing for the machine, but is slightly more readable for a human this way):
if($total_magazines >= 21 && $total_magazines <= 30)
{
$price = 1.75;
$total_amount = $total_magazines * $price;
}
In order to satisfy this condition, an integer should be one of those values: 21, 22, 23, ⋯ 29, 30.
What happens if you divide one of those numbers by ten? In a case of thirty, it's easy: the result is three. In a case of nine other numbers, you obtain a floating number as a result of the divisions: 2.1, 2.2, 2.3, ⋯ 2.9, 3.
PHP has a bunch of functions to work with floating point numbers. There is one of them which would, for all the numbers listed above (including the last one), give the exact same result. Could you figure out which one is that?
From your comments, it appears that you found it. You're right, ceil
returns 3 for 2.1, 2.2, etc., as well as 3. The next part of the answer was edited accordingly.
Removing code duplication
Now that, based on your comments, you found that you need to use ceil
, the previous piece of code can be rewritten this way:
if (ceil($total_magazines) == 3)
{
$price = 1.75;
$total_amount = $total_magazines * $price;
}
But wait, why would you write repetitive code again and again? in technical jargon, it's called code duplication, and there are only few cases where code duplication is fine. This is not one of them.
Let's see how we could change the code as to avoid code duplication. The goal here is to express the $total_amount
for different values of ceil
. There is, indeed, a simple computation which, given an integer, would give you the $total_amount
.
In order to figure it out, a simple approach is to list the values (i.e. the result of ceil
and the expected $price
.
- 1 → 0.75
- 2 → 1.25
- 3 → 1.75
- ⋮
- 9 → 4.75
If you can't spot the pattern (and if the original description of the problem doesn't help), a nice reflex would be to draw those points on a piece of paper and see how it looks like. Is it a curve? A straight line? Why? Once you found it, expressing it as a mathematical function shouldn't be an issue.
Handling the edge case
Once you get the values from 1 to 90 right, there is an edge case, expressed in your question by the condition $total_magazines > 91
(by the way, there is a mistake: it's rather $total_magazines >= 91
).
You can, obviously, have a conditional statement which would return a constant for any input superior or equal to 91, but going back to PHP mathematical functions, there is one which can help you to avoid the condition.
Is it a good thing to get rid of the condition? Maybe yes, maybe no. It depends on what your teacher was expecting in a specific context of this homework. In general, one would rather use a condition to make the edge case clearer, but maybe the expectation here was that the students would rely extensively on the mathematical functions in order to make an expression with no alternative control flow.