6
The problem: compare two arrays and give a dot whenever a number of one array is larger than the other. Ex)
A = [1, 2, 2]
B = [2, 1, 1]
result = [2, 1]
The answer I found:
function a($a, $b){
$a1 = 0;
$b1 = 0;
if ($a[0] > $b[0]) {
$a1++;
}
if ($a[1] > $b[1]) {
$a1++;
}
if ($a[2] > $b[2]) {
$a1++;
}
if ($a[0] < $b[0]) {
$b1++;
}
if ($a[1] < $b[1]) {
$b1++;
}
if ($a[2] < $b[2]) {
$b1++;
}
return [$a1, $b1];
}
$a = [5,6,7];
$b = [3,6,10];
var_dump(a($a, $b));
But I feel there is a lot of unnecessary repetition in this code. Some enhancement tip?
The tip is to use a same loop, that for the case the
for
will be the most appropriate– Isac