Assignment 2 - Calculate sum and product of a given array of numbers
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.
Click on the 'New array' button to enter some numbers, separated by comma, to calculate their sum and product.
numbers = [5, 6, 8, 4, 3, 2, 9, 2]
The sum is 0
The product is 0
$(document).ready(function() {
function getNumbers(element) {
var nums = element.text().split(','); // We get array of strings
nums = nums.map(function(currentValue, index, nums){
return currentValue * 1; // Type coercing, turning strings into numbers and stripping any whitespace
});
return nums; // Returns an array of numbers
}
function sum(array) {
var sum;
sum = array.reduce(function(previousValue, currentValue, index, array) {
return previousValue + currentValue;
});
return sum;
}
function multiply(array) {
var product;
product = array.reduce(function(previousValue, currentValue, index, array) {
return previousValue * currentValue;
});
return product;
}
function calculate(array) {
$('.result .sum').text(sum(array));
$('.result .product').text(multiply(array));
}
calculate(getNumbers($('.array-data')));
/**********************************************************************//***** EVENT HANDLING AND CONVENIENCE CODE *****//**********************************************************************/
$('.new-array').click(function() {
var currentNumbers = getNumbers($('.array-data'));
$('.array-data').hide();
$('.data input').show().val(currentNumbers);
$('input').focus().select();
});
$('.data').on('keydown', 'input', function(event) {
var key = event.which,
newArray,
oldArray = $('.array-data').text();
if (key === 13) {
newArray = $(this).val(); // On Enter we are reading input values
$(this).hide();
$('.array-data').show().text(newArray);
calculate(getNumbers($('.array-data')));
}
if (key === 27) {
$(this).hide();
$('.array-data').show().text(oldArray); // On Esc we are returning previous values into array
}
});
});