B. Dionys
B. Dionys

Reputation: 916

How to change a numeric value with given conditions

I had a test this week about php code. And there is a question still bothering me. Normally I wouldn't ask these questions here but this is it:

You have to make a divider sum with just min() max() and print()

You cannot use if statements (==, <=, >=).

You always have to divide the big number by the smaller number but if the small number is 0 or lower it has to be changed to 1.

My answer was something like:

$min = min($num1, $num2);
$max = max($num1, $num2);

Print($max/$min);

But how do you decide when the 0 has to be determined to 1 with the given conditions?

Upvotes: 1

Views: 72

Answers (1)

misorude
misorude

Reputation: 3431

but if the small number is 0 or lower it has to be changed to 1

So, in other words - the maximum of this number, and 1 …

max(-1, 1); // -> 1
max( 0, 1); // -> 1
max( 1, 1); // -> 1
max( 2, 1); // -> 2
...

Upvotes: 4

Related Questions