Help me solve this function problem

javascript

#1


I am trying to learn about closures in Javascript, and I have been working on this problem, and I just can't wrap my head around how it is supposed to work. Can anyone give me a possible solution and explain why it works like that?

/*
  Write a function called makeCounter that makes the following code work properly.
*/  


//Uncomment this once you make your function
//   var count = makeCounter();
//   count(); // 1
//   count(); // 2
//   count(); // 3
//   count(); // 4


#2

What are the instructions?


#3

The instructions are listed.

"Write a function called makeCounter that makes the following code work properly"


#4

I figured it out.

Here's how I did it:

function makeCounter() {
var num = 1;
return function () {
console.log(num++);
}
}

var count = makeCounter();
count(); // 1
count(); // 2
count(); // 3
count(); // 4


#5

Btw you shoudlt post anwsers in this fourms... so i would delete your post if i were you.


#6

The problem I listed was a problem from a 3rd party site (not this one).