π οΈ Section project: catch the star
Section 3: JavaScript: the actionsLesson 3.6 Β· β± 12 min
π― Project goal
A game: every time you click the star the points go up 1, and the star jumps to a random place.
HTML
<p>Score: <b id="score">0</b></p>
<div id="star">β</div>
CSS
body { font-family: Tahoma; margin: 0; height: 100vh; background: #1E1B4B; color: white; overflow: hidden; }
p { font-size: 22px; padding: 10px 16px; margin: 0; }
#star { position: absolute; left: 100px; top: 100px; font-size: 54px; cursor: pointer; }
JavaScript
let score = 0;
const star = document.getElementById("star");
star.onclick = function () {
score = score + 1;
document.getElementById("score").textContent = score;
star.style.left = Math.random() * 80 + "%";
star.style.top = Math.random() * 80 + "%";
};
π₯οΈ The page in Robo Web:

π‘ Tip
Math.random() is a random number from 0 to 1, so if we multiply it by 80 it becomes 0 to 80.π Another example: catch the star: you win at 5
When the points reach 5 the star turns into π.
HTML
<p>Points: <b id="score">0</b></p>
<div id="star">β</div>
CSS
body { font-family: Tahoma, sans-serif; margin: 0; height: 100vh; background: #1E1B4B; color: white; overflow: hidden; }
p { font-size: 22px; padding: 10px 16px; margin: 0; }
#star { position: absolute; left: 100px; top: 100px; font-size: 54px; cursor: pointer; }
JavaScript
let score = 0;
const star = document.getElementById("star");
star.onclick = function () {
score++;
document.getElementById("score").textContent = score;
if (score === 5) star.textContent = "π";
star.style.left = Math.random() * 80 + "%";
star.style.top = Math.random() * 80 + "%";
};
π₯οΈ The page in Robo Web:

βοΈ Practise
Catch the star.
βοΈ Practise here in the lessonβ Close the labor open the lab in a new page β
π― Challenge yourself
Easy
Change the star to π¦ and the background to dark green.
π Show the solutionπ Hide the solution
HTML
<p>Points: <b id="score">0</b></p>
<div id="star">π¦</div>
CSS
body { font-family: Tahoma, sans-serif; margin: 0; height: 100vh; background: #14532D; color: white; overflow: hidden; }
p { font-size: 22px; padding: 10px 16px; margin: 0; }
#star { position: absolute; left: 100px; top: 100px; font-size: 54px; cursor: pointer; }
JavaScript
let score = 0;
const star = document.getElementById("star");
star.onclick = function () {
score++;
document.getElementById("score").textContent = score;
star.style.left = Math.random() * 80 + "%";
star.style.top = Math.random() * 80 + "%";
};
π₯οΈ The page in Robo Web:

Result: a butterfly on green.
Medium
Every click makes the star a little smaller.
π Show the solutionπ Hide the solution
HTML
<p>Points: <b id="score">0</b></p>
<div id="star">β</div>
CSS
body { font-family: Tahoma, sans-serif; margin: 0; height: 100vh; background: #1E1B4B; color: white; overflow: hidden; }
p { font-size: 22px; padding: 10px 16px; margin: 0; }
#star { position: absolute; left: 100px; top: 100px; font-size: 54px; cursor: pointer; }
JavaScript
let score = 0;
let size = 54;
const star = document.getElementById("star");
star.onclick = function () {
score++;
document.getElementById("score").textContent = score;
size = size - 6;
star.style.fontSize = size + "px";
star.style.left = Math.random() * 80 + "%";
star.style.top = Math.random() * 80 + "%";
};
π₯οΈ The page in Robo Web:

Result: smaller after every click.
Hard
Clicking the background (outside the star) takes away a point.
π Show the solutionπ Hide the solution
HTML
<p>Points: <b id="score">0</b></p>
<div id="star">β</div>
CSS
body { font-family: Tahoma, sans-serif; margin: 0; height: 100vh; background: #1E1B4B; color: white; overflow: hidden; }
p { font-size: 22px; padding: 10px 16px; margin: 0; }
#star { position: absolute; left: 100px; top: 100px; font-size: 54px; cursor: pointer; }
JavaScript
let score = 0;
const star = document.getElementById("star");
function show() { document.getElementById("score").textContent = score; }
star.onclick = function (e) {
e.stopPropagation(); // so the page under it does not count this click
score++;
show();
star.style.left = Math.random() * 80 + "%";
star.style.top = Math.random() * 80 + "%";
};
document.body.onclick = function () {
score--;
show();
};
π₯οΈ The page in Robo Web:

Result: 3 stars and one wrong click: 2.

