2
function Increment()
{
  var counter = 100;
   function inner() 
    {
         counter += counter;
         return counter;
    }
}
var x = Increment();
console.log(x);
console.log(x);
console.log(x);

This is instruction of this code Use JavaScript Closure/self invoking method to do the following: a)Name the outer function as ‘Increment”. b)Store 100 as a counterin the outer function. c)Increment the counter by 100 in the inner functionand return. d)Call “Increment” three times and store the returned value in a variable each time. e)Log the final value in the web console (400 is the final valuefor the third call) , but function does not return even the first value.

Can you guys see where the problem is?

  • minor piece of feedback — avoid using `var`, and use `let` instead (https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var) – Ronak Shah Jun 15 '20 at 01:46
  • Are those the actual instructions, word for word? To get the result they want, you'd need to return the `inner` function, call `Increment` only once, storing its result (the inner function) in a variable, then call that returned function multiple times. –  Jun 15 '20 at 01:50
  • ...at least if closures are the objective, that would make more sense. –  Jun 15 '20 at 01:51

5 Answers5

0

You should call the "inner" function for it to execute.

And your Increment function Should return or update counter variable for it to take effect.

You should also declare the counter variable outside so you can increment it every time you call the "Increment" Function.

If you declare it inside it will just reset every time you call the "Increment" function.

you can do it like this.

function Increment()
{
    function inner() 
    {
        counter += 100;
        return counter;
    }
    return inner();
}

var counter = 100;

console.log(Increment());
console.log(Increment());
console.log(Increment());
Jovylle
  • 859
  • 7
  • 17
0

You will not only have to call the inner function but also return its result. The below code works

var counter = 100; 
function Increment()
    {
       function inner() 
        {
             counter += counter;
             return counter;
        }
    return inner();
    }
    var x = Increment();
    var y = Increment();
    console.log(x);
    console.log(y);
DpK
  • 100
  • 4
  • Thank you, your code works but it should increment every time I call the function. How should I save the value? – Seongwon Seo Jun 15 '20 at 01:46
  • function Increment() { var counter = 100; function inner() { counter += counter; return counter; } return inner(); } var x = Increment(); console.log(x); x = Increment(); console.log(x); x = Increment(); console.log(x); //This is how I changed it but it still gives me only 200. – Seongwon Seo Jun 15 '20 at 01:50
  • The variable counter should be initialized outside the function. I have updated the code accordingly. – DpK Jun 15 '20 at 01:52
0

You need to call the inner function three times, so you need to return the inner function in the outer function and call it.

function Increment()
{
  var counter = 100;
   function inner() 
    {
         counter += counter;
         return counter;
    }
    return inner;
}
var x = Increment();
console.log(x());
console.log(x());
console.log(x());

If the final value needs to be 400, you can rewrite your function a little bit like this:

function Increment() {
  var counter = 0;

  function inner() {
    counter += counter || 100;
    return counter;
  }
  return inner;
}
var x = Increment();
console.log(x());
console.log(x());
console.log(x());
MinusFour
  • 13,913
  • 3
  • 30
  • 39
  • function Increment() { var counter = 100; function inner() { counter += counter; return counter; } return inner(); } var x = Increment(); console.log(x); x = Increment(); console.log(x); x = Increment(); console.log(x); // I changed my code to this but it still gives me only 200, do you guys have some idea, it should keep increment. – Seongwon Seo Jun 15 '20 at 01:51
0

Increment() returns nothing, or undefined.

If you expect Increment to return the value of counter, simply add a return statement.

 function Increment() {
     let counter = 100;
     function inner() {
         counter += counter;
         return counter;
     }
     return counter;
}
let x = Increment();
console.log(x); // 100
console.log(x); // no change, since `Increment()` 
console.log(x); // was only called once

Keep in mind, inner is never called — if you wanted inner to do something, call inner(). Additionally, keep in mind that counter is declared within Increment(), meaning that each time you call it, it is redeclared (and reset to 100)

Ronak Shah
  • 936
  • 9
  • 17
0

Those instructions won't produce the result they anticipate.

Here's probably what they actually want.

function Increment() {
  var counter = 100;

  function inner() {
    counter += 100;
    return counter;
  }
  
  return inner;
}
var innerFunc = Increment();

var a = innerFunc();
var b = innerFunc();
var c = innerFunc();

console.log(c);

Two things changed in Increment.

First, the inner function increments the counter by 100. This is needed because counter is mutated, so counter += counter causes counter to increase by the most recently updated version.

Second, the inner function becomes the return value of Increment.

So you'd only call Increment once, and store its result in a variable. That variable holds the inner function you returned. Now every time you call that function, it increments the counter variable by 100, and returns its new value.

This is a proper demonstration of closures.