انتقل للمحتوى
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

The array and for…of

Section 6: Arrays and building the page with codeLesson 6.1 · ⏱ 9 min

An array is a list in JavaScript between square brackets [ ]. And for (const name of team) goes through its items one by one.

  • document.createElement("li") makes a new element.
  • li.textContent = … puts the text in it.
  • list.appendChild(li) adds it to the page.
HTML
<h2>My team</h2>
<ul id="list"></ul>
CSS
body { font-family: Tahoma, sans-serif; }
li { font-size: 20px; margin: 6px 0; }
JavaScript
const team = ["Ali", "Mona", "Omar", "Sara"];
const list = document.getElementById("list");

for (const name of team) {
  const li = document.createElement("li");
  li.textContent = "🤖 " + name;
  list.appendChild(li);
}
console.log("Team size:", team.length);
🖥️ The page in Robo Web:The page in Robo Web
🖥️ The Console in Robo Web:
Team size: 4

📌 Another example: the biggest number in the array

We go through all the marks, and keep the biggest.

🌐 The biggest number in the array
HTML
<p>Look at the Console 👇</p>
CSS
body { font-family: Tahoma, sans-serif; }
JavaScript
const grades = [70, 95, 60, 88];
let best = grades[0];

for (const g of grades) {
  if (g > best) best = g;
}
console.log("Highest mark:", best);
console.log("Number of marks:", grades.length);
🖥️ The page in Robo Web:The page in Robo Web
🖥️ The Console in Robo Web:
Highest mark: 95
Number of marks: 4
✍️ 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 ↗

Test yourself

1. How many li elements does the program make if we add two names to the array?
  1. Add your name to the array.
  2. Print the first name in the Console: team[0].

🎯 Challenge yourself

Easy

Print every colour in the colours array.

👀 Show the solution
🌐 Solution
HTML
<p>Console 👇</p>
CSS
body { font-family: Tahoma, sans-serif; }
JavaScript
const colors = ["Red", "Green", "Blue"];
for (const c of colors) {
  console.log(c);
}
🖥️ The page in Robo Web:The page in Robo Web
🖥️ The Console in Robo Web:
Red
Green
Blue

Result: three lines.

Medium

Show the fruits in a list on the page.

👀 Show the solution
🌐 Solution
HTML
<ul id="list"></ul>
CSS
body { font-family: Tahoma, sans-serif; }
JavaScript
const fruits = ["🍎 Apple", "🍌 Banana", "🍇 Grapes"];
const list = document.getElementById("list");
for (const f of fruits) {
  const li = document.createElement("li");
  li.textContent = f;
  list.appendChild(li);
}
🖥️ The page in Robo Web:The page in Robo Web

Result: a list of three.

Hard

The sum of the numbers and their average.

👀 Show the solution
🌐 Solution
HTML
<p>Console 👇</p>
CSS
body { font-family: Tahoma, sans-serif; }
JavaScript
const nums = [4, 8, 15, 13];
let sum = 0;
for (const n of nums) {
  sum += n;
}
console.log("Total:", sum);
console.log("Average:", sum / nums.length);
🖥️ The page in Robo Web:The page in Robo Web
🖥️ The Console in Robo Web:
Total: 40
Average: 10

Result: the sum is 40 and the average is 10.

Finished the lesson? Mark it so your progress counts.