Quiz.js 3.14 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
2
import React from 'react'
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
quiz    
Jiwon Yoon committed
4
5

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

class Quiz extends React.Component {
Jiwon Yoon's avatar
Jiwon Yoon committed
12
13
14
15
    constructor(props) {
        super(props)
        this.setQuestion = this.setQuestion.bind(this)
        this.answerbox = this.answerbox.bind(this)
16
        this.timer = this.timer.bind(this)
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
17
        // this.enterkey = this.enterkey(this)
Jiwon Yoon's avatar
Jiwon Yoon committed
18
19
20
21
        this.state = {
            ...question[0],
            i: 0,
            page: 0,
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
22
23
            question

Jiwon Yoon's avatar
Jiwon Yoon committed
24
        }
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
25
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
26
27
    setQuestion() {
        //값이 입력되지 않은채로 넘겨졌을 때 문제 해결 해야 함-sj-
28
        this.answerbox()
Jiwon Yoon's avatar
Jiwon Yoon committed
29
30
31
32
33
        this.setState({ ...question[this.state.i + 1], i: this.state.i + 1, page: this.state.page + 1 })
    }
    //answerbox - answer박스의 값을 네임리스트로 받아와서 값을 localstorage에 저장 
    answerbox() {
        let answers = document.getElementsByName('answer');
34
35
36
37
        for (let i = 0; i < answers.length; i++) {
            if (answers[i].checked === true) {
                localStorage.setItem((this.state.i + 1) + '번문제답', answers[i].value)
                localStorage.setItem((this.state.i + 1) + '번문제정답', question[this.state.i].A)
Jiwon Yoon's avatar
Jiwon Yoon committed
38
39
            }
        }
baesangjune's avatar
ing    
baesangjune committed
40
    }
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

    timer() {
        return (
            <Timer
                initialTime={10010}
                direction="backward"
                checkpoints={[
                    {
                        time: 0,
                        callback: this.setQuestion
                        //  history.go(1)
                    }
                ]}
            >
                {() => (
                    <>
                        <Timer.Seconds /> seconds
                    </>
                )}
            </Timer> /* npm i react-compound-timer */
        )
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
62
    }
baesangjune's avatar
ing    
baesangjune committed
63

Jiwon Yoon's avatar
Jiwon Yoon committed
64
    render() {
Jiwon Yoon's avatar
Jiwon Yoon committed
65
        return (
Jiwon Yoon's avatar
Jiwon Yoon committed
66
67
            <div className="Quiz">
                <h2>Q:{this.state.Q}</h2>
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
68
                <div>
69
70
71
72
73
                    {this.state.Choose.map((a) =>
                        <div>{this.state.N}
                            <input type="radio" name='answer' id={a} value={a} /*ref={this.textInput}  input 네임을 문제단위로 바꾸어주어야 함. */ />
                            <label for={a}>{a}</label>
                        </div>)
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
74

75
                    }
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
76
77
78
                </div>
                <div className="App" class='left'>정답을 입력하세요</div>

79

Jiwon Yoon's avatar
Jiwon Yoon committed
80
                {/* 마지막 질문일 경우 /end페이지로 이동, 그렇지 않을경우는 this.setQuestion발생 */}
81
82
                {(this.state.page === question.length - 1)
                    ? <Link to="/end"><button onClick={this.answerbox}>제출</button></Link>
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
83
                    : <button type="button" onClick={this.setQuestion} >다음</button>
84

Jiwon Yoon's avatar
Jiwon Yoon committed
85
                }
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
86
                {/* <input onKeyPress="this.enterkey()"/> */}
87

Jiwon Yoon's avatar
home    
Jiwon Yoon committed
88
                {this.timer()}
89

Jiwon Yoon's avatar
Jiwon Yoon committed
90
91
92
            </div>
        )
    }
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
93
94
}

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