Ψ§Ω†ΨͺΩ‚Ω„ Ω„Ω„Ω…Ψ­ΨͺΩˆΩ‰
Robo EgyptRobo Egypt
☰ Course contents
Robo Web: home
0. Lab tour1
1. HTML: the page's content7
2. CSS: the page's look6
3. JavaScript: the actions7
4. The project: the to-do list3
5. Pro CSS: shadows and movement5
6. Arrays and building the page with code3
7. Events and forms3
8. Objects and time3
9. Canvas: drawing and games3
10. The final project: the memory game3

πŸ› οΈ 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: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 πŸ†.

🌐 Catch the star: you win at 5
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:The page in Robo Web
✍️ Practise
Catch the star.
✍️ Practise here in the lessonor open the lab in a new page β†—

🎯 Challenge yourself

Easy

Change the star to πŸ¦‹ and the background to dark green.

πŸ‘€ Show the solution
🌐 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:The page in Robo Web

Result: a butterfly on green.

Medium

Every click makes the star a little smaller.

πŸ‘€ Show the solution
🌐 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:The page in Robo Web

Result: smaller after every click.

Hard

Clicking the background (outside the star) takes away a point.

πŸ‘€ Show the solution
🌐 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:The page in Robo Web

Result: 3 stars and one wrong click: 2.

Finished the lesson? Mark it so your progress counts.