fittaoee
fittaoee

Reputation: 145

Use closure to sum numbers

Currently I have a closure in JS that looks like the following:

var addTo = function(num){

   var add = function(inner){
      return inner + num;
   };
   return add;
};

var sum = new addTo(1);

My goal is to use the above closure to compute the sum from 1 all the way to 100 (i.e. sum = 1+2+3+...+99+100). Any help? I know a loop is needed, but am unsure of what should go inside the loop and how to use closure to achieve the goal. Thanks guys.

Upvotes: 0

Views: 5067

Answers (4)

crazyvraj
crazyvraj

Reputation: 37

Try

function sum(x) {
  var input = x;

  function add(y) {
    return input + y;
  }
  return add;
}
//var sum1 = sum(2);
//console.log(sum1(3)); // 5
console.log(sum(2)(3)); //5

Upvotes: 1

bentinata
bentinata

Reputation: 334

Maybe you want to use recursive instead of loops?

function addTo(initial) {

  function add(adder) {
    if (initial < 100) {
      initial+=1
      return add(adder+initial)
    }
    else {
      return adder
    }
  }

  return add(initial)
}

document.write(addTo(1))

As long as the initial values don't go over 100, it would just add with sum of all calculation before + itself + 1.

Upvotes: 0

RobG
RobG

Reputation: 147453

Currently I have a closure in JS that looks like the following:

All functions create closures, they're only remarkable when advantage is taken of them. ;-)

var addTo = function(num){

I don't know why function expressions are used when declarations are clearer (to me):

function addTo(num) {

then there's:

  var add = function(inner){
    return inner + num;
  }
  return add;
}

Which (sticking with an expression) can be:

  return function (inner) {return inner + num};
}

Then you call it with new:

var sum = new addTo(1);

which causes addTo to create a new object that is not used, so you might as well do:

var sum = addTo(1);

which produces exactly the same result. So:

function addTo(num) {
    return function (inner) {return inner + num};
}

var sum = addTo(1);

document.write(sum(3));

However, this is really just a version of Currying, so that sum will just add the supplied value to whatever was initially supplied to addTo.

If you want to add all the numbers from 0 to some limit, you just need a loop, no closure required:

function sumTo(num) {
  
  var total = 0;
  
  for (var i = 0; i <= num; i++) {
    total += i;
  }
  
  return total;
}

document.write(sumTo(5)); // 15

Note that supplying a negative number will result in an endless loop, you should protect against that (I'll leave it up to you to work out how).

Upvotes: 1

Ken Bellows
Ken Bellows

Reputation: 6940

It looks like the addTo() function returns another function into sum that will add whatever you pass it to the original number (or I assume that's what you meant to write; the first thing to do is change the statement inside of add() to use a += instead of just + to make sure you save the result).

Since you want to add each number from 2 to 100 (since you already passed 1 into addTo()), try writing a for loop that runs from 2 to 100 passing each one into the sum() function to add them all together. Here's an example:

var sum = addTo(1);
for (var i=2; i<100; i++) sum(i);
var result = sum(100);

Here I added 100 after the loop since I wanted to grab the final result. You could also add 100 in the loop and use sum(0) to get the result without changing it after the loop.

Upvotes: -1

Related Questions