In this article, we will talk about a JavaScript problem which is Range and Sum Function in JavaScript. This problem is in chapter 4 of Eloquent JavaScript book which very famous for JS of course. So we will understand this problem and also solve the problem with very basic logic.
Range and Sum Function Problem
The introduction of this book alluded to the following as a nice way to compute the sum of a range of numbers:console.log(sum(range(1, 10)));
Write a range
function that takes two arguments, start
and end
, and returns an array containing all the numbers from start
up to (and including) end
.
Next, write a sum
function that takes an array of numbers and returns the sum of these numbers. Run the previous program and see whether it does indeed return 55.
Range and Sum Function Solution
Okay, so now let’s make a function range where we will create an array, and we assigned the value of start in the variable named cnt. So here we have used a loop so that we can assign all the values in an array. We used while loop, we can also go with for loop, here loop will run till cnt will be less or equal to end. In this we are just pushing the value of cnt in the array and incremented cnt, lastly we just return this array.
Now let’s create another function named sum to get the addition of the array’s values, here we have passed the array as a parameter in the function. Now we have a variable total with initial 0 value, and again we used while loop till the array’s length in which we are just poping the array and adding in the total variable, and we are returning total. Lastly, we are using console.log with both function calls, here we are passing range(1,10) as an argument in the sum function call. So the range function will be called first, and it will return the array which is going to be used by the sum function.
const range = function(start, end) {
var arr = [],
cnt = start;
while(cnt<=end){
arr.push(cnt);
cnt++;
}
return arr;
}
const sum = function(arr){
var total = 0;
while(arr.length>0){
total = total + arr.pop();
}
return total;
}
console.log(sum(range(1,10)));
Range and Sum Function Solution: Alternative
As we know, JavaScript is flexible, which means we can have multiple ways to solve one problem. So let’s talk about alternative way, here we are eliminating loop completely by using function recursion. In range function, we declared a blank array and start assigned to cnt, now we declared another function named increaseCnt which take cnt as parameter. In this, we will check where cnt is greater than end, then just push end value and return array. Otherwise, push cnt, and we will call this function again with increased cnt value. This thing works similar as loop until the array won’t return.
Now in the sum function, we applied to reduce method on the array and added a callback function with two parameters and we are just adding these both and return them.
const range = function(start, end) {
var arr = [],
cnt = start;
function increaseCnt(cnt){
if(cnt>=end){
return arr.push(end);
}
else{
arr.push(cnt);
increaseCnt(++cnt);
}
}
increaseCnt(cnt);
return arr;
}
const sum = function(arr){
return arr.reduce(function(tot,val){
return tot + val;
})
}
console.log(sum(range(1,10)));