Skip to content

Instantly share code, notes, and snippets.

@mgiagante
Created October 31, 2020 12:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mgiagante/1e00f2a6db740f48b45d4c176b74ecb8 to your computer and use it in GitHub Desktop.
Save mgiagante/1e00f2a6db740f48b45d4c176b74ecb8 to your computer and use it in GitHub Desktop.
// 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