CodeAgeBy Sitezack
JavaScript for MakersDOM and events0 XP
Chapter progress0/6

Lesson 6 of 7

One function, several buttons

When several buttons do almost the same thing, write a single function and call it with different arguments.

Keep the state (the counter) in a variable declared with var outside the function, and re-display it after every change.

var count = 0;
function change(step) {
  count = count + step;
  document.getElementById('count').textContent = count;
}

🎯 Your goal

Build a counter: a count variable at 0, a change(step) function that updates it and refreshes the <span id="count">. Wire #plus to change(1) and #minus to change(-1).

PRO lesson

This advanced chapter is part of CodeAge PRO. Unlock it to keep your adventure going.

Discover PRO