Skip to content

Instantly share code, notes, and snippets.

@kosalvann
Created September 23, 2015 10:49
Show Gist options
  • Save kosalvann/a993f75f16aea86b3a97 to your computer and use it in GitHub Desktop.
Save kosalvann/a993f75f16aea86b3a97 to your computer and use it in GitHub Desktop.
Define a function sum() and a function multiply() that sums and multiplies (respectively) all the numbers in an array of numbers. For example, sum([1,2,3,4]) should return 10, and multiply([1,2,3,4]) should return 24.
// Define a function sum() and a function multiply() that sums and
// multiplies (respectively) all the numbers in an array of numbers.
// For example, sum([1,2,3,4]) should return 10, and
// multiply([1,2,3,4]) should return 24.
// https://jsfiddle.net/a7ukhbdn/
// Set addition
function sum(numbers) {
var total = 0;
for (var i = 0; i < numbers.length; i++) {
total += numbers[i];
}
return total;
}
// Print out the total to the console
console.log(sum([1,2,3,4]));
// Set multiplication
function multiply(numbers) {
var total = 1;
for (var i = 0; i < numbers.length; i++) {
total = (total * numbers[i]);
}
return total;
}
// Print out the total to the console
console.log(multiply([1,2,3,4]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment