Assignment 13 CB - Simple adding
Have the function simpleAdding(num) add up all the numbers from 1 to num. For the test cases, the parameter num will be any number from 1 to 1000.
Enter an positive integer in the input field below.
$(document).ready(function() {
function simpleAdding(num) {
if (num === 0) {
return num;
} else {
return num + simpleAdding(num - 1);
}
}
$('.calculate').click(function() {
var numString = $('.data input').val(),
numValue = numString * 1;
if (numString === '' || isNaN(numValue) || parseInt(numValue) !== numValue || parseInt(numValue) < 0) {
$('.result p').html('Please enter an positive integer in the input field.');
} else {
$('.result p').html('Numbers from 1 to ' + numValue + ', add up to: <span>' + simpleAdding(numValue) + '</span>');
}
});
});