-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
129 lines (110 loc) · 3.91 KB
/
script.js
File metadata and controls
129 lines (110 loc) · 3.91 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// ============================================================================
// Constellation Lab
// (with sound functionality)
// ============================================================================
// Utility function to play a click sound.
function playClickSound() {
const sound = document.getElementById('clickSound');
if (sound) {
sound.currentTime = 0;
sound.play();
}
}
// Get the current year once the page is fully loaded.
let currentYear;
document.addEventListener('DOMContentLoaded', () => {
currentYear = new Date().getFullYear();
});
// Part 1: JavaScript Basics
const form = document.getElementById('userForm');
const nameInput = document.getElementById('nameInput');
const ageInput = document.getElementById('ageInput');
const greetOut = document.getElementById('greet');
form.addEventListener('submit', (e) => {
e.preventDefault();
playClickSound();
const name = String(nameInput.value).trim();
const age = Number(ageInput.value);
let group;
if (age < 13) group = 'child';
else if (age < 18) group = 'teen';
else if (age < 60) group = 'adult';
else group = 'senior';
greetOut.textContent = name
? `Hello, ${formatName(name)} — you are categorized as: ${group}.`
: 'Please provide your name.';
console.log({ name, age, group });
});
// Part 2: Functions
function formatName(name) {
if (!name) return '';
return name[0].toUpperCase() + name.slice(1).toLowerCase();
}
function calculateAge(birthYear) {
return currentYear - birthYear;
}
const birthYearInput = document.getElementById('birthYearInput');
const birthYearBtn = document.getElementById('birthYearBtn');
const ageOut = document.getElementById('ageOut');
birthYearBtn.addEventListener('click', () => {
playClickSound();
const birthYear = Number(birthYearInput.value);
if (!birthYear || birthYear > currentYear) {
ageOut.textContent = 'Please enter a valid birth year.';
} else {
const age = calculateAge(birthYear);
ageOut.textContent = `Your age is: ${age} years old.`;
console.log(`Age calculated: ${age}`);
}
});
// Part 3: Loops
const listOut = document.getElementById('listOut');
document.getElementById('countdownBtn').addEventListener('click', () => {
playClickSound();
listOut.innerHTML = '';
listOut.classList.add('left-align');
listOut.classList.remove('right-align');
let n = 5;
while (n >= 0) {
const li = document.createElement('li');
li.textContent = `Countdown: ${n}`;
listOut.appendChild(li);
n--;
}
});
document.getElementById('starsBtn').addEventListener('click', () => {
playClickSound();
listOut.innerHTML = '';
listOut.classList.add('right-align');
listOut.classList.remove('left-align');
for (let i = 1; i <= 6; i++) {
const li = document.createElement('li');
li.textContent = '★'.repeat(i);
listOut.appendChild(li);
}
});
// Part 4: DOM Manipulation
const domBtn1 = document.getElementById('domBtn1');
const domBtn2 = document.getElementById('domBtn2');
// We now get the whole section element by its ID
const domSection = document.getElementById('dom');
const toggleText = document.getElementById('toggleText');
domBtn1.addEventListener('click', () => {
playClickSound();
// Toggle the new class on the section, not the inner div
domSection.classList.toggle('panel--colored');
console.log('Panel background color toggled.');
});
domBtn2.addEventListener('click', () => {
playClickSound();
toggleText.classList.toggle('hidden');
const isHidden = toggleText.classList.contains('hidden');
console.log(`Paragraph visible: ${!isHidden}`);
});
// Part 5: Theme Switcher
const themeBtn = document.getElementById('themeBtn');
themeBtn.addEventListener('click', () => {
playClickSound();
document.body.classList.toggle('light-theme');
console.log('Theme toggled. Check the body element class.');
});