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

const QnA = [
JeongYeonwoo's avatar
updated    
JeongYeonwoo committed
8
9
10
    { 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 }
11
12
]

JeongYeonwoo's avatar
updated    
JeongYeonwoo committed
13
14


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

JeongYeonwoo's avatar
updated    
JeongYeonwoo committed
23
24
    function handleQuestion() {
        setQuestion({ ...QnA[question.i + 1], i: question.i + 1, page: question.page + 1 })
25
    }
JeongYeonwoo's avatar
updated    
JeongYeonwoo committed
26

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

        let checked_number = ev.target.id;

        localStorage.setItem('' + (question.N), Number(checked_number) + 1)
        localStorage.setItem('정답' + (question.N), QnA[question.N - 1].A)

36
37
    }
    return (
JeongYeonwoo's avatar
JeongYeonwoo committed
38
        <>
JeongYeonwoo's avatar
updated    
JeongYeonwoo committed
39
40
41
            <div style={{ fontSize: '80px', marginBottom: '100px', textAlign: "center", backgroundColor: 'yellow' }}>미적분학 퀴즈</div>
            <img src={fight} style={{ position: "absolute", left: "1050px", top: '200px' }} alt="lion" />
            <div className="Quiz" >
JeongYeonwoo's avatar
JeongYeonwoo committed
42
                <h1><img src={logo} style={{ marginLeft: "450px" }} width='75' height='75' alt='question' /> <span style={{ fontSize: "75px", marginLeft: "30px" }}>{question.Q}</span></h1>
JeongYeonwoo's avatar
updated    
JeongYeonwoo committed
43
                {/* <div style={{ marginTop: "30px", marginBottom: "30px", marginLeft:'450px', fontSize:'40px' }}>정답을 선택하세요</div> */}
JeongYeonwoo's avatar
JeongYeonwoo committed
44
45
46
47
48

                <div>
                    <form>
                        {question.Choose.map((a, index) =>
                            <div key={index}>
JeongYeonwoo's avatar
updated    
JeongYeonwoo committed
49
                                <input type="radio" name='answer' id={index} value={a} onClick={handleChange} style={{ marginLeft: "475px", width: "25px", height: "25px" }} />
JeongYeonwoo's avatar
JeongYeonwoo committed
50
51
52
53
54
                                <label htmlFor={a} style={{ fontSize: "40px", marginLeft: "22px" }}>{a}</label>
                            </div>
                        )}
                        <input hidden type="submit" value="확인" />    {/*버튼 숨김*/}
                    </form>
JeongYeonwoo's avatar
updated    
JeongYeonwoo committed
55
                    <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)
JeongYeonwoo's avatar
JeongYeonwoo committed
56
                        ? <Link to="/end">제출</Link>
JeongYeonwoo's avatar
updated    
JeongYeonwoo committed
57
                        : <button type="button" onClick={handleQuestion} style={{ width: "85px", height: "40px" }} >다음</button>
JeongYeonwoo's avatar
JeongYeonwoo committed
58
59
60
61

                    }</span>
                    </p>
                </div>
62
63
64
65




JeongYeonwoo's avatar
JeongYeonwoo committed
66

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

JeongYeonwoo's avatar
JeongYeonwoo committed
72
73
74
75
76
77
78
            } */}

                {/* <input onKeyPress="this.enterkey()"/> */}


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

JeongYeonwoo's avatar
JeongYeonwoo committed
96
97
            </div>
        </>
98
99
100
101
102
103
104
105
    )



}


export default Quiz;