A dice roller is a small app that rolls one or more dice and shows the result. It is a great beginner project because it teaches you random numbers, DOM updates, and a little animation — and the result is something you can actually use in a board game night.
How does it work? The app picks a random number between 1 and 6 when you click a button, shows the matching dice face, and keeps a history of your rolls so you can see your luck over time.
<!-- index.html -->
<div id="dice">🎲</div>
<p id="result">Click Roll to start</p>
<button id="roll">Roll the Dice</button>
<button id="roll-two">Roll Two Dice</button>
<h3>Roll History</h3>
<ul id="history"></ul>
// script.js — roll the dice with random numbers
const diceFaces = ['⚀', '⚁', '⚂', '⚃', '⚄', '⚅'];
const diceEl = document.getElementById('dice');
const resultEl = document.getElementById('result');
const historyEl = document.getElementById('history');
function roll(count) {
const rolls = [];
for (let i = 0; i < count; i++) {
rolls.push(Math.floor(Math.random() * 6) + 1);
}
const total = rolls.reduce((a, b) => a + b, 0);
diceEl.textContent = rolls.map((r) => diceFaces[r - 1]).join(' ');
resultEl.textContent =
count === 1 ? `You rolled ${total}` : `You rolled ${rolls.join(' + ')} = ${total}`;
const item = document.createElement('li');
item.textContent = `${rolls.join(', ')} (total ${total})`;
historyEl.prepend(item);
}
document.getElementById('roll').addEventListener('click', () => roll(1));
document.getElementById('roll-two').addEventListener('click', () => roll(2));
/* style.css — make the dice big and fun */
#dice {
font-size: 96px;
text-align: center;
transition: transform 0.15s ease;
}
#dice.rolling {
transform: rotate(360deg) scale(1.1);
}
This project teaches you the most useful function in beginner JavaScript — Math.random() — plus the habit of keeping a history list, which shows up in calculators, games, and trackers of every kind.
The limits are worth knowing. Math.random() is fine for games, but it is not secure randomness — never use it for passwords, tokens, or anything security-related. And a dice roller is only fair if every face has an equal chance, which Math.floor(Math.random() * 6) + 1 guarantees.
Start today: build the page above, roll the dice ten times, and check the history. Then add one extra feature — a button that rolls three dice at once, or a counter that shows how many times you rolled a six.
Top comments (0)