Skip to content
Permalink
42247479f3
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
54 lines (37 sloc) 902 Bytes
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sum of a Range</title>
</head>
<body>
<script>
function range( start, end, increment ) {
var result = [];
if ( increment == undefined )
increment = 1;
numLoops = Math.abs( (end - start)/ increment ) + 1 ;
for ( var i = 0; i < numLoops; i ++ ) {
result.push( start );
start += increment;
}
return result;
}
function sum( numArray ) {
var arrayTotal = 0;
numLoops = numArray.length;
for ( var i = 0; i < numLoops; i ++ ) {
arrayTotal += numArray[i];
}
return arrayTotal;
}
console.log(range(1, 10));
// → [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log(range(5, 2, -1));
// → [5, 4, 3, 2]
console.log(sum(range(1, 10)));
// → 55
</script>
</body>
</html>