JonnyDevs
JonnyDevs

Reputation: 45

how many times 5 fits in to a set number

I've written some basic messy code to solve this issue but I'm sure there must be a much better way of doing it. Here's my attempt:

$max = 17.9;
$box=0;
$x=0;
for (;;) {
   if ($box > $max) break;
   $box+=5;
   $x++;
}
$bonus = $x-1;
echo "$bonus points added!";

So here we see that 5 fit's in to 17.9 3 times.

Upvotes: 2

Views: 168

Answers (1)

John Conde
John Conde

Reputation: 219834

You made this way harder then it needed to be

$bonus = intval(17 / 5); 
  1. Divide 17 by 5
  2. Remove the remainder (floor() works here as well)

Upvotes: 7

Related Questions