Quiz.js 2.12 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
quiz    
Jiwon Yoon committed
3
4

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

class Quiz extends React.Component {
Jiwon Yoon's avatar
Jiwon Yoon committed
11
12
13
14
15
16
17
18
19
    constructor(props) {
        super(props)
        this.setQuestion = this.setQuestion.bind(this)
        this.answerbox = this.answerbox.bind(this)
        this.state = {
            ...question[0],
            i: 0,
            page: 0,
        }
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
20
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    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;
                localStorage.setItem('answer-' + i, checked_value)
            }
        }
baesangjune's avatar
ing    
baesangjune committed
38
39
    }

Jiwon Yoon's avatar
Jiwon Yoon committed
40
    render() {
Jiwon Yoon's avatar
Jiwon Yoon committed
41
        return (
Jiwon Yoon's avatar
Jiwon Yoon committed
42
43
44
45
46
47
48
49
50
            <div className="Quiz">
                <h2>Q:{this.state.Q}</h2>
                {this.state.Choose.map((a) =>
                    <div>
                        <input type="radio" name='answer' id={a} value={a} /*ref={this.textInput}*/ />
                        <label for={a}>{a}</label>
                    </div>)
                }
                <div className="App">정답을 입력하세요</div>
baesangjune's avatar
   
baesangjune committed
51
                
Jiwon Yoon's avatar
Jiwon Yoon committed
52
53
54
55
56
                {/* 마지막 질문일 경우 /end페이지로 이동, 그렇지 않을경우는 this.setQuestion발생 */}
                {(this.state.page === question.length-1)
                    ? <Link to="/end">제출</Link>
                    : <button type="button" onClick={this.setQuestion}>다음</button>
                }
Jiwon Yoon's avatar
Jiwon Yoon committed
57
58
59
            </div>
        )
    }
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
60
61
}

Jiwon Yoon's avatar
Jiwon Yoon committed
62
export default Quiz;