Para este código a desarrollar vamos a tomar el ejercicio propuesto del newsletter de «rendezvous with cassidoo«. El newsletter se titula «There is nothing more truly artistic than to love people.» – Vincent Van Gogh. El ejercicio propuesto dice:
You are given an array of arrays, where each inner array represents the runs scored by each team in an inning of a baseball game: [[home1, away1], [home2, away2], …]. Write a function that returns an object with the total runs for each team, which innings each team led, and who won the game.
(Se le proporciona un arreglo de arreglos, donde cada arreglo interno representa las carreras anotadas por cada equipo en una entrada de un juego de béisbol: [[home1, away1], [home2, away2], …]. Escriba una función que devuelva un objeto con el total de carreras de cada equipo, qué entradas lideró cada equipo y quién ganó el juego.)
const innings = [[1, 0], [2, 2], [0, 3], [4, 1]];
> analyzeBaseballGame(innings)
> {
homeTotal: 7,
awayTotal: 6,
homeLedInnings: [1, 2, 4],
awayLedInnings: [3],
winner: "home"
}
Solucion planteada PHP 8
Aqui va mi solucion en PHP 8
$item) {
$data["homeTotal"] += $item[0];
$data["awayTotal"] += $item[1];
if($data["homeTotal"] >= $data["awayTotal"]){
$data["homeLedInnings"][] = $key + 1;
} else {
$data["awayLedInnings"][] = $key + 1;
$data["winner"] = "away";
}
}
return json_encode($data, JSON_PRETTY_PRINT);
}
const INNINGS = [[1, 0], [2, 2], [0, 3], [4, 1]];
print_r(analyzeBaseballGame(INNINGS));