arrays - Award Budget Cuts (PHP)
one text
Solution:
You should try and reduce the repetition of calculations, so the maximum budget can be worked out before the foreach
loop. Also rather than have an if
to check if it's above this value, then use min
to take the lowest of the entry and the maximum budget
function findGrantsCap($grantsArray, $newBudget) : array {
$maxBudget = $newBudget / count($grantsArray);
$newArray = [];
foreach($grantsArray as $entry) {
$newArray[] = min ( $entry, $maxBudget);
}
return $newArray;
}
Source