Last active
June 28, 2020 06:05
-
-
Save jeremymouzin/beb6c92bba23e179d2040d8a16fc90ba to your computer and use it in GitHub Desktop.
Challenge 3 du Challenge JavaScript 10 jours de Scrimba
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Version lisible pour débutants | |
function alternatingSums(tableau) { | |
let sommePaires = 0; | |
let sommeImpaires = 0; | |
for (let i = 0; i < tableau.length; i++) { | |
if (i % 2 === 0) { | |
sommePaires = sommePaires + tableau[i]; | |
} else { | |
sommeImpaires = sommeImpaires + tableau[i]; | |
} | |
} | |
return [sommePaires, sommeImpaires]; | |
} | |
// Version avancée compacte | |
function alternatingSums(tableau) { | |
return tableau.reduce((res,nombre,i) => { | |
res[i % 2] += nombre; | |
return res; | |
}, [0,0]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment