Quiz.js 3.39 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 = [
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
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)
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
16
17
        this.timer=this.timer.bind(this)
        // 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
28
29
30
31
32
33
34
35
36
37
38
39
    setQuestion() {
        //값이 입력되지 않은채로 넘겨졌을 때 문제 해결 해야 함-sj-
        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');
        let count = answers.length
        // let checked_index = -1;
        let checked_value = '';
        for (let i = 0; i < count; i++) {
            if (answers[i].checked) {
                // checked_index = i;
                checked_value = answers[i].value;
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
40
41
                localStorage.setItem(this.state.i+'번문제답', checked_value)
                localStorage.setItem(this.state.i+'번문제정답', question[i-1].A)
Jiwon Yoon's avatar
Jiwon Yoon committed
42
43
            }
        }
baesangjune's avatar
ing    
baesangjune committed
44
    }
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    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 */
       )
    }
    // enterkey() {
    //     if ( window.event === 13 ) {
    //         alert("Enter Key 입력 감지 \n함수 실행.");
    //     }
    // }
baesangjune's avatar
ing    
baesangjune committed
71

Jiwon Yoon's avatar
Jiwon Yoon committed
72
    render() {
Jiwon Yoon's avatar
Jiwon Yoon committed
73
        return (
Jiwon Yoon's avatar
Jiwon Yoon committed
74
75
            <div className="Quiz">
                <h2>Q:{this.state.Q}</h2>
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
76
77
                {this.answerbox()}
                <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
78
                {this.state.Choose.map((a) =>
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
79
80
                    <div>{this.state.N}
                        <input type="radio" name='answer'  id={a} value={a} /*ref={this.textInput}  input 네임을 문제단위로 바꾸어주어야 함. */ />
Jiwon Yoon's avatar
Jiwon Yoon committed
81
                        <label for={a}>{a}</label>
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
82
83
                    </div>)                   

Jiwon Yoon's avatar
Jiwon Yoon committed
84
                }
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
85
86
87
                </div>
                <div className="App" class='left'>정답을 입력하세요</div>

baesangjune's avatar
   
baesangjune committed
88
                
Jiwon Yoon's avatar
Jiwon Yoon committed
89
90
91
                {/* 마지막 질문일 경우 /end페이지로 이동, 그렇지 않을경우는 this.setQuestion발생 */}
                {(this.state.page === question.length-1)
                    ? <Link to="/end">제출</Link>
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
92
93
                    : <button type="button" onClick={this.setQuestion} >다음</button>
                    
Jiwon Yoon's avatar
Jiwon Yoon committed
94
                }
Jiwon Yoon's avatar
home    
Jiwon Yoon committed
95
96
97
98
                {/* <input onKeyPress="this.enterkey()"/> */}
                                
                {this.timer()}
               
Jiwon Yoon's avatar
Jiwon Yoon committed
99
100
101
            </div>
        )
    }
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
102
103
}

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