Created
September 23, 2015 10:49
-
-
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.
This file contains 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
// 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