Menu
Coddy logo textTech

Count in Steps

Part of the Fundamentals section of Coddy's React journey — lesson 24 of 42.

The setter takes any new value: you're not limited to +1. Add 5, double it, reset to zero:

setPoints(points + 5);
setPoints(points * 2);
setPoints(0);

Buttons for different amounts are just different handlers on the same state:

<button onClick={() => setPoints(points + 5)}>+5</button>
<button onClick={() => setPoints(0)}>Reset</button>
challenge icon

Challenge

Easy

A game gives 5 points per coin.

  1. Add points state starting at 0 and show it in the span with id="points".
  2. The Collect coin button (id="coin") adds 5 per click.

Try it yourself

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="styles.css" />
    </head>
    <body>
        <div id="root"></div>
        <script type="module" src="main.jsx"></script>
    </body>
</html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals