Skip to content

Instantly share code, notes, and snippets.

@Domminique
Created October 23, 2017 17:36
Show Gist options
  • Save Domminique/2e2eb17d2f0e40dd07ddd02fec0d93b5 to your computer and use it in GitHub Desktop.
Save Domminique/2e2eb17d2f0e40dd07ddd02fec0d93b5 to your computer and use it in GitHub Desktop.
Code Challenge
//Challenge #1 >Write a function sum_even(a) that sums up the even numbers in the list(array)
function sum_even(a) {
b = [];
for (var i = 0; i < a.length; ++i) {
if ((a[i] % 2) === 0) {
b.push(a[i]); //gives us the array for even nums
}
}
//adding the even nums
return b.reduce((a,b) => a+b, 0);
}
console.log(sum_even([10, 5, 6, 1]));
//---------------------------------------------------------------------------------------------------------------
//Challenge #2 > Write a function super_sum(a) that sums up all the digits that make up the numbers in the array.
function super_sum(a){
var digitSums = a.map(function(a) {
return Array.prototype.slice.call(a.toString()).map(Number).reduce(function(b, c) {
return b + c; // gets each individual digit
});
});
//adding the all the numbers in the array together
return digitSums.reduce((a,b) => a+b, 0);
}
console.log(super_sum([120, 13, 45]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment