π οΈ 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-typefrom HTML in JavaScript withb.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:

π₯οΈ What happens on the stage?
We clicked βFoodβ and only three cards appeared: watermelon, mango and pizza.
- Add a third type: βSportβ β½π, and a button for it.
- 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.
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:

βοΈ Practise
Try the example here: copy the code into its tabs, change it, and watch the page change.
βοΈ Practise here in the lessonβ Close the labor open the lab in a new page β
π― Challenge yourself
Easy
Show all the animals as cards.
π Show the solutionπ Hide the 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:

Result: four cards.
Medium
Write the number of legs under each name.
π Show the solutionπ Hide the 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:

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π Hide the 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:

Result: we typed βphβ: the elephant appeared.

