JavaScript code not working

Please explain whey the following does not execute.
var counterModule = (function() {
	var counter = 0;
		
	return {
		incrementCounter: function () {
			 counter++;
       alert(counter++);
		},
		resetCounter: function () {
			counter = 0;
			alert("counter value prior to reset: " + counter);
		}
	};
}

counterModule.incrementCounter();
counterModule.incrementCounter();
counterModule.incrementCounter();
counterModule.resetCounter();

Open in new window


https://jsfiddle.net/isogunro/f2qjoqbs/

Thanks!
LVL 5
IsaacSharePoint Client Side DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zephyr_hex (Megan)DeveloperCommented:
You are missing a )
If you use F12 console, you can see the error.

You also need paranthesis when you call counterModule

counterModule().incrementCounter();

Here's an updated Fiddle:  https://jsfiddle.net/zephyr_hex/q8eowfja/

Next... you may want to use a globally scoped counter variable (move it outside the counterModule function).  Otherwise, the increment isn't additive.
0
Robert SchuttSoftware EngineerCommented:
Actually I would expect the intended pattern to be an IIFE because the whole idea would be to create a self-contained class or 'module' to handle the counter functionality. By executing it more than once you lose that functionality. By changing it to use a global variable you're taking a step back instead of forward (in terms of code design).

The corrected code would be:
var counterModule = (function() {
	var counter = 0;
		
	return {
		incrementCounter: function () {
			 counter++;
       alert(counter++);
		},
		resetCounter: function () {
			counter = 0;
			alert("counter value prior to reset: " + counter);
		}
	};
})(); // note the result of the function execution being assigned to the variable instead of a reference to the function itself

counterModule.incrementCounter();
counterModule.incrementCounter();
counterModule.incrementCounter();
counterModule.resetCounter();

Open in new window

Some more errors are left in there (mainly related to the alerts) but I feel they can be resolved more easily once you have the module pattern implemented and working.

EDIT: updated fiddle here
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IsaacSharePoint Client Side DeveloperAuthor Commented:
Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.