I have tried to solve this problem: you are given two integers(A and B). You need to make an output from A to B(inclusively) in ascending order if A < B and in descending order if B < A. The problem is that it has to be done using a recursive function. I have come up with the following:
function showAB($a, $b) {
if ($a > $b) {
if ($a == $b) {
return $a;
} else {
return $a . "<br>" . showAB($a - 1, $b);
}
}
else {
if ($a == $b) {
return $a;
} else {
return $a . "<br>" . showAB($a + 1, $b);
}
}
}
However, I was told that the solution to this problem can be done in 2 lines of code or less, so my question is about optimizing the function I already have.
return ($a > $b) ? $a."<br>".showAB($a-1, $b) : ($a == $b) ? $a : $a."<br>".showAB($a+1, $b);I haven't tested this, but this should execute the same as the code above and is only a single line! But it's nowhere near as readable as what you have...return ($a != $b) ? $a . '<br>' . showAB($a + ($a < $b ? 1 : -1), $b) : $a . '<br>';3v4l.org/bba8k$awill never== $bbecause if$a > $bthey are obviously not equal to each other