Ψ§Ω†ΨͺΩ‚Ω„ Ω„Ω„Ω…Ψ­ΨͺΩˆΩ‰
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: the emoji garden

Section 6: Arrays and building the page with codeLesson 6.3 · ⏱ 13 min
🎯 Project goal
Cards of animals and food, and filter buttons: All, Animals, Food.
  • Every item in the array is an object with properties: { emoji, name, type }.
  • The function show(type) clears the cards, then draws only the ones of that type.
  • We read data-type from HTML in JavaScript with b.dataset.type.
  • In text between ` ` we put variables with ${ }.
HTML
<h2>Emoji garden</h2>
<div id="filters">
  <button data-type="all">All</button>
  <button data-type="animal">Animals</button>
  <button data-type="food">Food</button>
</div>
<div id="cards"></div>
CSS
body { font-family: Tahoma, sans-serif; }
#cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-top: 12px; }
.card { background: #f2f4ff; border-radius: 14px; padding: 10px; text-align: center; font-size: 34px; }
.card small { display: block; font-size: 14px; }
button { font-size: 16px; padding: 6px 14px; border-radius: 999px; border: 1px solid #2557c9; background: white; }
JavaScript
const items = [
  { emoji: "🦁", name: "Lion", type: "animal" },
  { emoji: "🐘", name: "Elephant", type: "animal" },
  { emoji: "πŸͺ", name: "Camel", type: "animal" },
  { emoji: "πŸ‰", name: "Watermelon", type: "food" },
  { emoji: "πŸ₯­", name: "Mango", type: "food" },
  { emoji: "πŸ•", name: "Pizza", type: "food" },
];

function show(type) {
  const box = document.getElementById("cards");
  box.innerHTML = "";
  for (const it of items) {
    if (type === "all" || it.type === type) {
      box.innerHTML += `<div class="card">${it.emoji}<small>${it.name}</small></div>`;
    }
  }
}

document.querySelectorAll("#filters button").forEach(function (b) {
  b.onclick = function () { show(b.dataset.type); };
});
show("all");
πŸ–₯️ The page in Robo Web:The page in Robo Web
πŸ–₯️ What happens on the stage?
We clicked β€œFood” and only three cards appeared: watermelon, mango and pizza.
  1. Add a third type: β€œSport” βš½πŸ€, and a button for it.
  2. Write above the cards how many of them are showing.

πŸ“Œ Another example: the zoo

We draw a card for every animal, and a button shows only the ones with four legs.

🌐 The zoo
HTML
<button id="four">Four legs</button>
<div id="cards"></div>
CSS
body { font-family: Tahoma, sans-serif; }
#cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.card { background: #f2f4ff; border-radius: 14px; padding: 10px; text-align: center; font-size: 34px; }
.card small { display: block; font-size: 14px; }
button { font-size: 16px; padding: 6px 14px; }
JavaScript
const animals = [
  { emoji: "🦁", name: "Lion", legs: 4 },
  { emoji: "🐧", name: "Penguin", legs: 2 },
  { emoji: "🐍", name: "Snake", legs: 0 },
  { emoji: "🐘", name: "Elephant", legs: 4 },
];

function show(list) {
  const box = document.getElementById("cards");
  box.innerHTML = "";
  for (const a of list) {
    box.innerHTML += `<div class="card">${a.emoji}<small>${a.name}</small></div>`;
  }
}
show(animals);

document.getElementById("four").onclick = function () {
  show(animals.filter(a => a.legs === 4));
};
πŸ–₯️ The page in Robo Web:The page in Robo Web
✍️ Practise
Try the example here: copy the code into its tabs, change it, and watch the page change.
✍️ Practise here in the lessonor open the lab in a new page β†—

🎯 Challenge yourself

Easy

Show all the animals as cards.

πŸ‘€ Show the solution
🌐 Solution
HTML
<div id="cards"></div>
CSS
body { font-family: Tahoma, sans-serif; }
#cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.card { background: #f2f4ff; border-radius: 14px; padding: 10px; text-align: center; font-size: 34px; }
.card small { display: block; font-size: 14px; }
button { font-size: 16px; padding: 6px 14px; }
JavaScript
const animals = [
  { emoji: "🦁", name: "Lion", legs: 4 },
  { emoji: "🐧", name: "Penguin", legs: 2 },
  { emoji: "🐍", name: "Snake", legs: 0 },
  { emoji: "🐘", name: "Elephant", legs: 4 },
];
for (const a of animals) {
  document.getElementById("cards").innerHTML += `<div class="card">${a.emoji}<small>${a.name}</small></div>`;
}
πŸ–₯️ The page in Robo Web:The page in Robo Web

Result: four cards.

Medium

Write the number of legs under each name.

πŸ‘€ Show the solution
🌐 Solution
HTML
<div id="cards"></div>
CSS
body { font-family: Tahoma, sans-serif; }
#cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.card { background: #f2f4ff; border-radius: 14px; padding: 10px; text-align: center; font-size: 34px; }
.card small { display: block; font-size: 14px; }
button { font-size: 16px; padding: 6px 14px; }
JavaScript
const animals = [
  { emoji: "🦁", name: "Lion", legs: 4 },
  { emoji: "🐧", name: "Penguin", legs: 2 },
  { emoji: "🐍", name: "Snake", legs: 0 },
  { emoji: "🐘", name: "Elephant", legs: 4 },
];
for (const a of animals) {
  document.getElementById("cards").innerHTML += `<div class="card">${a.emoji}<small>${a.name}</small><small>🦡 ${a.legs}</small></div>`;
}
πŸ–₯️ The page in Robo Web:The page in Robo Web

Result: the penguin 🦡 2.

Hard

A search box: you type some letters and the animals with those letters in their name appear.

πŸ‘€ Show the solution
🌐 Solution
HTML
<input id="q" placeholder="Search">
<div id="cards"></div>
CSS
body { font-family: Tahoma, sans-serif; }
#cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.card { background: #f2f4ff; border-radius: 14px; padding: 10px; text-align: center; font-size: 34px; }
.card small { display: block; font-size: 14px; }
button { font-size: 16px; padding: 6px 14px; }
input { font-size: 18px; margin-bottom: 10px; }
JavaScript
const animals = [
  { emoji: "🦁", name: "Lion", legs: 4 },
  { emoji: "🐧", name: "Penguin", legs: 2 },
  { emoji: "🐍", name: "Snake", legs: 0 },
  { emoji: "🐘", name: "Elephant", legs: 4 },
];

function show() {
  const q = document.getElementById("q").value;
  const box = document.getElementById("cards");
  box.innerHTML = "";
  for (const a of animals) {
    if (a.name.includes(q)) {
      box.innerHTML += `<div class="card">${a.emoji}<small>${a.name}</small></div>`;
    }
  }
}
document.getElementById("q").oninput = show;
show();
πŸ–₯️ The page in Robo Web:The page in Robo Web

Result: we typed β€œph”: the elephant appeared.

Finished the lesson? Mark it so your progress counts.