-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
156 lines (135 loc) · 4.77 KB
/
script.js
File metadata and controls
156 lines (135 loc) · 4.77 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
152
153
154
155
156
const weights = [1.0, 1.2, 1.2, 1.3, 1.3, 1.3, 1.5, 1.5, 1.5, 1.7, 1.7, 1.7, 2, 2, 2];
let quizData = null;
let currentLang = 'ko';
let currentIndex = 0;
let answers = [];
const introSection = document.getElementById('intro');
const quizSection = document.getElementById('quiz-section');
const resultSection = document.getElementById('result-section');
const questionCard = document.getElementById('question-card');
const optionsDiv = document.getElementById('options');
const progressText = document.getElementById('progress-text');
// ✅ 언어 버튼 클릭 → 언어 적용 + 시작 버튼 표시
document.querySelectorAll('.lang-btn').forEach(btn => {
btn.addEventListener('click', () => {
currentLang = btn.dataset.lang;
// 버튼 선택 상태 스타일 적용
document.querySelectorAll('.lang-btn').forEach(b => b.classList.remove('selected'));
btn.classList.add('selected');
// 언어 JSON 불러오기
fetch(`lang/${currentLang}.json`)
.then(res => res.json())
.then(data => {
quizData = data;
applyLanguage(); // 전체 텍스트 업데이트
document.getElementById('start-btn').style.display = 'block';
});
});
});
// ✅ 시작 버튼 클릭 시 퀴즈 시작
document.getElementById('start-btn').addEventListener('click', () => {
if (!quizData) return; // 언어 선택 안 했으면 동작 X
startQuiz();
});
// ✅ 전체 텍스트 적용 함수
function applyLanguage() {
document.getElementById('main-title').innerText = quizData.title;
document.querySelector('.intro-text').innerHTML = quizData.intro.replace(/\n/g, '<br>');
document.getElementById('start-btn').innerText = quizData.start;
document.getElementById('prev-btn').innerText = quizData.back;
document.getElementById('restart-btn').innerText = quizData.restart;
document.getElementById('share-btn').innerText = quizData.share;
}
function startQuiz() {
introSection.style.display = 'none';
quizSection.style.display = 'block';
resultSection.style.display = 'none';
currentIndex = 0;
answers = Array(quizData.questions.length).fill(null);
showQuestion();
}
function showQuestion() {
const q = quizData.questions[currentIndex];
questionCard.innerHTML = q.replace(/\n/g, '<br>');
optionsDiv.innerHTML = '';
progressText.innerText = `${currentIndex + 1} / ${quizData.questions.length}`;
quizData.options.forEach((opt, idx) => {
const btn = document.createElement('button');
btn.innerText = opt;
btn.className = 'option-btn';
btn.onclick = () => {
answers[currentIndex] = idx;
if (currentIndex < quizData.questions.length - 1) {
currentIndex++;
showQuestion();
} else {
showResult();
}
};
optionsDiv.appendChild(btn);
});
document.getElementById('prev-btn').style.display = currentIndex > 0 ? 'inline-block' : 'none';
}
document.getElementById('prev-btn').onclick = () => {
if (currentIndex > 0) {
currentIndex--;
showQuestion();
}
};
function checkImmediateDanger() {
const dangerConditions = [
{ index: 9, minScore: 2 },
{ index: 10, minScore: 2 },
{ index: 11, minScore: 1 },
{ index: 12, minScore: 1 },
{ index: 13, minScore: 1 },
{ index: 14, minScore: 1 }
];
return dangerConditions.some(cond => answers[cond.index] !== null && answers[cond.index] >= cond.minScore);
}
function showResult() {
quizSection.style.display = 'none';
resultSection.style.display = 'block';
let score = 0;
answers.forEach((a, i) => {
if (a !== null) score += a * weights[i];
});
let resultKey = 'safe';
let scoreClass = 'score-safe';
const isImmediateDanger = checkImmediateDanger();
if (isImmediateDanger) {
resultKey = 'danger';
scoreClass = 'score-danger';
score += 66;
if (score > 100) score = 100;
} else if (score >= 66) {
resultKey = 'danger';
scoreClass = 'score-danger';
} else if (score >= 46) {
resultKey = 'warning';
scoreClass = 'score-warning';
} else if (score >= 26) {
resultKey = 'caution';
scoreClass = 'score-caution';
}
score = Math.ceil(score);
document.getElementById('result-message').innerHTML = `
<div class="score-box ${scoreClass}">${score}${quizData.score}</div>
<div class="description">${quizData.results[resultKey]}</div>
`;
}
document.getElementById('restart-btn').onclick = () => {
resultSection.style.display = 'none';
introSection.style.display = 'block';
};
document.getElementById('main-title').onclick = () => {
resultSection.style.display = 'none';
quizSection.style.display = 'none';
introSection.style.display = 'block';
};
document.getElementById('share-btn').onclick = () => {
const url = window.location.href;
navigator.clipboard.writeText(url).then(() => {
alert(quizData?.copy_success || '링크가 복사되었습니다!');
});
};