challenge-algorithms-Ana-Ca.../07-soma-dos-pares/index.html

37 lines
1.0 KiB
HTML
Raw Permalink Normal View History

2021-08-18 21:00:43 +00:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
/*
Faça um algoritmo que retorne a soma de todos os numeros pares de um array
*/
function sum(numbers) {
// implementar logica aqui
let sumOfEven = 0;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 === 0) {
sumOfEven = sumOfEven + numbers[i];
}
}
return sumOfEven;
2021-08-18 21:00:43 +00:00
}
// Resultados esperados
console.log(sum([4, 6, 12, 5]), 22) // 22
console.log(sum([9, 234, 312, 999, 21, 2311]), 546) // 546
console.log(sum([533, 234, 23423, 32, 48876]), 49142) // 49142
console.log(sum([5, 4, 3, 2, 1]), 6) // 6
</script>
</body>
</html>