๐ js
"use strict";
function Quiz(questions) {
this.score = 0;
this.questions = questions;
this.questionIndex = 0;
}
Quiz.prototype.correctAnswer = function(answer) {
return answer == this.questions[this.questionIndex].answer;
}
function Question(text, choice, answer) {
this.text = text;
this.choice = choice;
this.answer = answer;
}
var questions = [
new Question('๋ค์ ์ค ๊ณผ์ผ์ด ์๋ ๊ฒ์?', ['ํ์ธ์ ํ', '๋ธ๊ธฐ', 'ํ ๋งํ ', '์ฌ๊ณผ'], 'ํ ๋งํ '),
new Question('๋ค์ ์ค ํค๊ฐ 170cm ์ดํ์ธ ์ฌ๋ฐฐ์ฐ๋?', ['Blake Lively', 'Julia Roberts', 'Gwyneth Kate Paltrow', 'Natalie Portman'], 'Natalie Portman'),
new Question('๋ค์ ์ค ๋ฐฐ์ฐ "์ด๋ณํ"์ ์ถ์ฐ์์ด ์๋ ๊ฒ์?', ['๊ณต๋๊ฒฝ๋น๊ตฌ์ญ JSA', '๋๊ตฌ๋ ๋น๋ฐ์ ์๋ค', '๊ทธ ํด ์ฌ๋ฆ', '์ ์ธ๊ณ'], '์ ์ธ๊ณ'),
new Question('๋ค์ CSS ์์ฑ ์ค ๊ธ์์ ๊ตต๊ธฐ๋ฅผ ๋ณ๊ฒฝํ๋ ์์ฑ์?', ['font-size', 'font-style', 'font-weight', 'font-variant'], 'font-weight'),
new Question('๋ค์ ์ค ์ฐ๋ฆฌ๋๋ผ ๋ณด๋ฌผ1ํธ๋?', ['๋๋๋ฌธ', '์๋๋ฌธ', '๋จ๋๋ฌธ', '๋ถ๋๋ฌธ'], '๋๋๋ฌธ'),
new Question('๋ค์ ์ค ๋ํ๋ฏผ๊ตญ์์ ๊ฐ์ฅ ๋จ์ชฝ์ ์๋ ์ฌ์?', ['๋
๋', '๋ง๋ผ๋', '์ ์ฃผ๋', '์ธ๋ฆ๋'], '๋ง๋ผ๋'),
new Question('๋ค์ ์ค ํํ๋ฒ์ด ๋ค๋ฅธ ํ๋๋?', ['์ด๊ฒ์ ์๋ฆฌ ์๋ ์์ฐ์ฑ', '์ฆ๊ฑฐ์ด ๋น๋ช
', '์ฐฌ๋ํ ์ฌํ์ ๋ด', '์ฃฝ์ด๋ ์๋ ๋๋ฌผ ํ๋ฆฌ์ค๋ฆฌ๋ค'], '์ฃฝ์ด๋ ์๋ ๋๋ฌผ ํ๋ฆฌ์ค๋ฆฌ๋ค')
];
var quiz = new Quiz(questions);
function updateQuiz() {
var question = document.getElementById('question');
var idx = quiz.questionIndex + 1;
var choice = document.querySelectorAll('.btn');
question.innerHTML = '๋ฌธ์ ' + idx + ') ' + quiz.questions[quiz.questionIndex].text;
.
for (var i = 0; i < 7; i++) {
choice[i].innerHTML = quiz.questions[quiz.questionIndex].choice[i];
}
}
var btn = document.querySelectorAll('.btn');
function checkAnswer(i) {
btn[i].addEventListener('click', function() {
var answer = btn[i].innerText;
if (quiz.correctAnswer(answer)) {
alert('์ ๋ต์
๋๋ค!');
quiz.score++;
} else {
alert('ํ๋ ธ์ต๋๋ค!');
}
if (quiz.questionIndex < quiz.questions.length - 1) {
quiz.questionIndex++;
updateQuiz();
} else {
result();
}
});
}
function result() {
var quizDiv = document.getElementById('quiz');
var per = parseInt((quiz.score * 100) / quiz.questions.length);
var txt = '<h1>๊ฒฐ๊ณผ</h1>' + '<h2 id="score">๋น์ ์ ์ ์: ' + quiz.score + '/' + quiz.questions.length + '<br><br>' + per + '์ ' + '</h2>';
.
quizDiv.innerHTML = txt;
if (per < 40) {
txt += '<h2>๋ ๋ถ๋ฐํ์ธ์</h2>';
quizDiv.innerHTML = txt;
} else if (per >= 40 && per < 70 ) {
txt += '<h2>๋ฌด๋ํ ์ ์๋ค์</h2>'
quizDiv.innerHTML = txt;
} else if (per >= 70) {
txt += '<h2>ํ๋ฅญํฉ๋๋ค</h2>'
quizDiv.innerHTML = txt;
}
}
for (var i = 0; i < btn.length; i++) {
checkAnswer(i);
}
updateQuiz();
var Body = {
setBackColor : function(color) {
document.querySelector('body').style.backgroundColor=color;
},
setColor : function(color) {
document.querySelector('body').style.color=color;
}
}
var Link = {
setColor : function (color){
var alist=document.querySelectorAll('a');
var i = 0;
while(i < alist.length){
alist[i].style.color=color;
i=i+1;
}
}
}
function handler(self){
var target=document.querySelector('body')
if(self.value==='night') {
Body.setBackColor('black');
Body.setColor('white');
self.value='day';
Link.setColor('powderblue');
} else {
Body.setBackColor('white');
Body.setColor('black');
self.value='night';
Link.setColor('white');
}
}