Quiz.js 4.19 KB
Newer Older
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
1
import React, { useState } from 'react'
Jiwon Yoon's avatar
Jiwon Yoon committed
2
import { Link } from 'react-router-dom';
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
3
import Timer from 'react-compound-timer'; // 타이머쓰기위해 import
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
4
5
import logo from './img/img_question.png'
import fight from './img/img_quiz.png'
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
6

Jiwon Yoon's avatar
g    
Jiwon Yoon committed
7

Jiwon Yoon's avatar
a    
Jiwon Yoon committed
8
const QnA = [
baesangjune's avatar
0805    
baesangjune committed
9
10
11
    { Q: "6 X 4 = ?", Choose: [6, 12, 18, 24], A: "4", N: 1 },
    { Q: "3 + 3 = ?", Choose: [2, 4, 6, 8], A: "3", N: 2 },
    { Q: "3 - 1 = ?", Choose: [1, 2, 3, 4], A: "2", N: 3 }
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
12
13
]

Jiwon Yoon's avatar
a    
Jiwon Yoon committed
14
function Quiz() {
baesangjune's avatar
0805    
baesangjune committed
15
    let [question, setQuestion] = useState({
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
16
17
18
19
20
        ...QnA[0],
        i: 0,
        page: 0,
    })
    let [selected, setSelected] = useState("")  //선택한 답을 보여줄 것들
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
21

baesangjune's avatar
0805    
baesangjune committed
22
23
    function handleQuestion() {
        setQuestion({ ...QnA[question.i + 1], i: question.i + 1, page: question.page + 1 })
Jiwon Yoon's avatar
Jiwon Yoon committed
24
    }
baesangjune's avatar
0805    
baesangjune committed
25

Jiwon Yoon's avatar
a    
Jiwon Yoon committed
26
27
28
    let handleChange = (ev) => {
        ev.preventDefault()  //새로고침 안되도록
        setSelected(ev.target.value)  //selected값 변경
baesangjune's avatar
0805    
baesangjune committed
29
30
31
32
33
34

        let checked_number = ev.target.id;

                localStorage.setItem(''+(question.N), Number(checked_number)+1)
                localStorage.setItem('정답' + (question.N), QnA[question.N-1].A)
                
baesangjune's avatar
ing    
baesangjune committed
35
    }
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
36
37
    return (
        <>
baesangjune's avatar
0805    
baesangjune committed
38
39
            <div style={{ fontSize: '80px', marginBottom: '100px', textAlign: "center", backgroundColor: 'yellow' }}>미적분학 퀴즈</div>
            <img src={fight} style={{ position: "absolute", left: "1050px", top: '200px' }} alt="lion" />
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
40
41
42
            <div className="Quiz" >
                <h1><img src={logo} style={{ marginLeft: "450px" }} width='75' height='75' alt='question' /> <span style={{ fontSize: "75px", marginLeft: "30px" }}>{question.Q}</span></h1>
                {/* <div style={{ marginTop: "30px", marginBottom: "30px", marginLeft:'450px', fontSize:'40px' }}>정답을 선택하세요</div> */}
43

Jiwon Yoon's avatar
home    
Jiwon Yoon committed
44
                <div>
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
45
46
47
                    <form>
                        {question.Choose.map((a, index) =>
                            <div key={index}>
baesangjune's avatar
0805    
baesangjune committed
48
                                <input type="radio" name='answer' id={index} value={a} onClick={handleChange} style={{ marginLeft: "475px", width: "25px", height: "25px" }}  />
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
49
50
51
52
53
54
55
                                <label htmlFor={a} style={{ fontSize: "40px", marginLeft: "22px" }}>{a}</label>
                            </div>
                        )}
                        <input hidden type="submit" value="확인" />    {/*버튼 숨김*/}
                    </form>
                    <p style={{ fontSize: "40px", marginLeft: "480px" }}>Answer :<span style={{ color: "green", fontWeight: "bold", fontSize: "90px", marginLeft: "30px" }}> {selected}</span>  {/* 선택한  보여줌 */}<span style={{ marginLeft: "50px" }}>            {(question.page === QnA.length - 1)
                        ? <Link to="/end">제출</Link>
baesangjune's avatar
0805    
baesangjune committed
56
                        : <button type="button" onClick={handleQuestion} style={{ width: "85px", height: "40px" }} >다음</button>
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
57

Jiwon Yoon's avatar
a    
Jiwon Yoon committed
58
59
                    }</span>
                    </p>
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
60
61
                </div>

baesangjune's avatar
home    
baesangjune committed
62
63
64
65




baesangjune's avatar
0805    
baesangjune committed
66
                {/* 마지막 질문일 경우 /end페이지로 이동, 그렇지 않을경우는 this.handleQuestion발생 */}
baesangjune's avatar
home    
baesangjune committed
67
68
                {/* {(question.page === QnA.length - 1)
                ? <Link to="/end">제출</Link>
baesangjune's avatar
0805    
baesangjune committed
69
                : <button type="button" onClick={handleQuestion} >다음</button>
baesangjune's avatar
home    
baesangjune committed
70
71
72

            } */}

baesangjune's avatar
baesangjune committed
73
                {/* <input onKeyPress="this.enterkey()"/> */}
baesangjune's avatar
home    
baesangjune committed
74
75


Jiwon Yoon's avatar
a    
Jiwon Yoon committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
                <p style={{ textAlign: "center", fontSize: "30px", color: "crimson" }}>
                    <Timer
                        initialTime={3600000}
                        direction="backward"
                        checkpoints={[
                            {
                                time: 0,
                                callback: <Link to="/end">제출</Link>
                                //  history.go(1)
                            }
                        ]}
                    >
                        {() => (
                            <>
                                <Timer.Minutes /> : <Timer.Seconds></Timer.Seconds> / 60 : 00                        </>
                        )}
                    </Timer>     {/* npm i react-compound-timer */}
                </p>
94

Jiwon Yoon's avatar
a    
Jiwon Yoon committed
95
96
97
            </div>
        </>
    )
98
99
100



Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
101
102
}

Jiwon Yoon's avatar
a    
Jiwon Yoon committed
103

Jiwon Yoon's avatar
home    
Jiwon Yoon committed
104
export default Quiz;