-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11. Arrays.html
More file actions
65 lines (53 loc) · 1.73 KB
/
Copy path11. Arrays.html
File metadata and controls
65 lines (53 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS Array Basics</title>
<style>
body {
font-family: system-ui;
padding: 20px;
line-height: 1.5;
}
button {
padding: 10px 20px;
cursor: pointer;
}
.array-container {
margin-top: 20px;
border: 1px solid #ccc;
padding: 15px;
border-radius: 8px;
}
code {
background: #eee;
padding: 2px 4px;
border-radius: 4px;
}
</style>
</head>
<body>
<h2>JavaScript Array Explorer</h2>
<button onclick="renderArray()">Update Output</button>
<div id="output" class="array-container">
Click the button to visualize the array!
</div>
<script>
function renderArray() {
// 1. Defining the array
const fruits = ["Apple", "Banana", "Mango", "Grapes"];
const display = document.getElementById("output");
// 2. Using Template Literals (Backticks ``) for cleaner HTML generation
// 3. Using .map() is more "Modern JS" than a standard for-loop
const listItems = fruits.map((fruit, index) => {
return `<li>Index <code>[${index}]</code> holds the value: <strong>${fruit}</strong></li>`;
}).join('');
display.innerHTML = `
<p><strong>Array Length:</strong> ${fruits.length}</p>
<ul>${listItems}</ul>
<p><em>Pro tip: Arrays in JS start at index 0!</em></p>
`;
}
</script>
</body>
</html>