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

baesangjune's avatar
home    
baesangjune committed
7
const QnA = [
baesangjune's avatar
0805    
baesangjune 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 }
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
11
12
]

baesangjune's avatar
baesangjune committed
13
14
15
let Answers = []
let Solutions = [4, 3, 2]
localStorage.setItem('Solutions', JSON.stringify(Solutions))
baesangjune's avatar
0805    
baesangjune committed
16

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

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

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

baesangjune's avatar
.    
baesangjune committed
33
        Answers[question.N - 1] = Number(ev.target.id) + 1
baesangjune's avatar
0805    
baesangjune committed
34

baesangjune's avatar
baesangjune committed
35
        localStorage.setItem('Answers', JSON.stringify(Answers))
baesangjune's avatar
ing    
baesangjune committed
36
    }
baesangjune's avatar
home    
baesangjune committed
37
38
    return (
        <>
baesangjune's avatar
baesangjune committed
39
            <div style={{ fontSize: '80px', marginBottom: '100px', textAlign: "center", backgroundColor: 'red' }}>미적분학 퀴즈</div>
baesangjune's avatar
0805    
baesangjune committed
40
            <img src={fight} style={{ position: "absolute", left: "1050px", top: '200px' }} alt="lion" />
baesangjune's avatar
0    
baesangjune committed
41
            <div className="Quiz" >
baesangjune's avatar
home    
baesangjune 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>
baesangjune's avatar
0    
baesangjune committed
43
                {/* <div style={{ marginTop: "30px", marginBottom: "30px", marginLeft:'450px', fontSize:'40px' }}>정답을 선택하세요</div> */}
baesangjune's avatar
home    
baesangjune committed
44

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

baesangjune's avatar
.    
baesangjune committed
60
                        }</span>
baesangjune's avatar
home    
baesangjune committed
61
                    </p>
baesangjune's avatar
baesangjune committed
62
                </div>
baesangjune's avatar
timer    
baesangjune committed
63

baesangjune's avatar
home    
baesangjune committed
64
65
66
67




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

            } */}

baesangjune's avatar
baesangjune committed
75
                {/* <input onKeyPress="this.enterkey()"/> */}
baesangjune's avatar
home    
baesangjune committed
76
77
78
79


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

Jiwon Yoon's avatar
Jiwon Yoon committed
97
            </div>
baesangjune's avatar
home    
baesangjune committed
98
99
100
101
102
        </>
    )



Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
103
104
}

baesangjune's avatar
home    
baesangjune committed
105
106

export default Quiz;