-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
151 lines (133 loc) · 4.28 KB
/
script.js
File metadata and controls
151 lines (133 loc) · 4.28 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const form = document.getElementById('personality-test');
const resultDiv = document.getElementById('result');
const progressBar = document.getElementById('progress-bar');
const progressText = document.getElementById('progress-text');
const resetBtn = document.getElementById('reset-btn');
const themeToggleBtn = document.getElementById('theme-toggle');
const QUESTION_NAMES = ['q1', 'q2', 'q3'];
function getAnsweredCount() {
let answered = 0;
for (const name of QUESTION_NAMES) {
const checked = document.querySelector(`input[name="${name}"]:checked`);
if (checked) answered += 1;
}
return answered;
}
function updateProgress() {
const answered = getAnsweredCount();
const total = QUESTION_NAMES.length;
const pct = Math.round((answered / total) * 100);
if (progressBar) progressBar.style.width = `${pct}%`;
if (progressText) progressText.textContent = `Progress: ${answered}/${total} answered`;
}
function clearErrors() {
document.querySelectorAll('fieldset.error').forEach(fs => fs.classList.remove('error'));
}
function validateAndFocusFirstUnanswered() {
clearErrors();
for (const name of QUESTION_NAMES) {
const fieldset = document.querySelector(`input[name="${name}"]`)?.closest('fieldset');
const checked = document.querySelector(`input[name="${name}"]:checked`);
if (!checked && fieldset) {
fieldset.classList.add('error');
fieldset.scrollIntoView({ behavior: 'smooth', block: 'center' });
return false;
}
}
return true;
}
document.addEventListener('change', (e) => {
if (e.target && e.target.matches('input[type="radio"]')) {
updateProgress();
const fieldset = e.target.closest('fieldset');
if (fieldset) fieldset.classList.remove('error');
}
});
form.addEventListener('submit', function (event) {
event.preventDefault();
if (!validateAndFocusFirstUnanswered()) return;
const q1 = document.querySelector('input[name="q1"]:checked').value;
const q2 = document.querySelector('input[name="q2"]:checked').value;
const q3 = document.querySelector('input[name="q3"]:checked').value;
// Calculate the personality type based on the user's answers
let result;
if (q1 === 'yes' && q2 === 'yes' && q3 === 'yes') {
result = 'Introvert 😶 ';
} else if (q1 === 'no' && q2 === 'no' && q3 === 'no') {
result = 'Extrovert 🫠 ';
} else {
result = 'Ambivert ✨ ';
}
// Display the resulting personality type in the page with a simple animation
resultDiv.style.opacity = '0';
resultDiv.textContent = `Your personality type is ${result}.`;
requestAnimationFrame(() => {
resultDiv.style.transition = 'opacity 300ms ease';
resultDiv.style.opacity = '1';
});
saveAnswers();
});
// Reset handler
if (resetBtn) {
resetBtn.addEventListener('click', () => {
form.reset();
clearErrors();
resultDiv.textContent = '';
localStorage.removeItem(ANSWERS_STORAGE_KEY);
updateProgress();
});
}
// Theme toggle with persistence
const THEME_STORAGE_KEY = 'site-theme';
function applyTheme(theme) {
if (theme === 'dark') {
document.body.classList.add('dark');
} else {
document.body.classList.remove('dark');
}
}
function loadTheme() {
const saved = localStorage.getItem(THEME_STORAGE_KEY);
if (saved) applyTheme(saved);
}
if (themeToggleBtn) {
themeToggleBtn.addEventListener('click', () => {
const isDark = document.body.classList.toggle('dark');
localStorage.setItem(THEME_STORAGE_KEY, isDark ? 'dark' : 'light');
});
}
// Persist answers and restore on load
const ANSWERS_STORAGE_KEY = 'personality-answers';
function saveAnswers() {
const answers = {};
for (const name of QUESTION_NAMES) {
const checked = document.querySelector(`input[name="${name}"]:checked`);
answers[name] = checked ? checked.value : null;
}
localStorage.setItem(ANSWERS_STORAGE_KEY, JSON.stringify(answers));
}
function restoreAnswers() {
try {
const raw = localStorage.getItem(ANSWERS_STORAGE_KEY);
if (!raw) return;
const answers = JSON.parse(raw);
for (const name of QUESTION_NAMES) {
const value = answers?.[name];
if (value) {
const input = document.querySelector(`input[name="${name}"][value="${value}"]`);
if (input) input.checked = true;
}
}
} finally {
updateProgress();
}
}
document.addEventListener('change', (e) => {
if (e.target && e.target.matches('input[type="radio"]')) {
saveAnswers();
}
});
// Init
loadTheme();
restoreAnswers();
updateProgress();