Created
October 31, 2020 12:27
-
-
Save mgiagante/1e00f2a6db740f48b45d4c176b74ecb8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// La funcion llamada 'sumaTodosPrimos' recibe como argumento un array de enteros. | |
// y debe devolver la suma total entre todos los numeros que sean primos. | |
// Pista: un número primo solo es divisible por sí mismo y por 1 | |
// Nota: Los números 0 y 1 NO son considerados números primos | |
// Ej: | |
// sumaTodosPrimos([1, 5, 2, 9, 3, 4, 11]) devuelve 5 + 2 + 3 + 11 = 21 | |
function sumaTodosPrimos(numeros) { | |
const primos = numeros.filter(numero => esPrimo(numero)); | |
const sumador = (accumulador, valorActual) => accumulador + valorActual; | |
return primos.reduce(sumador); | |
} | |
const esPrimo = numero => { | |
for(let i = 2; i < numero; i++) | |
if(numero % i === 0) return false; | |
return numero > 1; | |
} | |
console.log(sumaTodosPrimos([1, 5, 2, 9, 3, 4, 11])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment