Quiz.js 2.91 KB
Newer Older
baesangjune's avatar
Quiz.js    
baesangjune committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import React from 'react';
// import logo from './logo.svg';
import './Quiz.css';


const question = [
  { 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: "" }
]

class Quiz extends React.Component {
  constructor(props) {
    super(props)
    // this.setAnswer = this.setAnswer.bind(this)
    this.setQuestion = this.setQuestion.bind(this)
    this.ShowQuiz = this.ShowQuiz.bind(this)
baesangjune's avatar
ing    
baesangjune committed
18
19
20
21
22
23
24
25
    this.answerbox = this.answerbox.bind(this)
    this.state = {
      ...question[0],
      i: 0,
      page: 0,
    }
    this.textInput = React.createRef()
        
baesangjune's avatar
Quiz.js    
baesangjune committed
26
27
  }
  ShowQuiz() {
baesangjune's avatar
ing    
baesangjune committed
28
    this.setState({ page: 1 })
baesangjune's avatar
Quiz.js    
baesangjune committed
29
30
  }
  setQuestion() {
baesangjune's avatar
ing    
baesangjune committed
31
32
    this.setState({ ...question[this.state.i + 1], i: this.state.i + 1 })

baesangjune's avatar
Quiz.js    
baesangjune committed
33
  }
baesangjune's avatar
ing    
baesangjune committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

  answerbox() {
    let answers = document.getElementsByName('answer');
    let count = answers.length
    // var checked_index = -1;
    var checked_value = '';

    for (var i = 0; i < count; i++) {
      if (answers[i].checked) {
        // checked_index = i;
        checked_value = answers[i].value;
        localStorage.setItem('answer'+i, checked_value)
      }
    }
    // alert('선택된 항목 인덱스: ' + checked_index + '\n선택된 항목 값: ' + checked_value);


    // if (document.getElementsByName("answer")[i].checked === true) {
      
    //   alert(document.getElementsByName("answer")[i].value);
    // }


  }

baesangjune's avatar
Quiz.js    
baesangjune committed
59
60
61
62
63
64
65
  // setAnswer(e) {
  //   question[this.state.i]["A"] = e.target.value
  //   this.setState({ v: e.target.value })
  //   console.log(this.state)
  //   console.log(question)
  // }
  render() {
baesangjune's avatar
ing    
baesangjune committed
66
67
    if (this.state.page === 1) {
      if (this.state.i === question.length) {
baesangjune's avatar
Quiz.js    
baesangjune committed
68
69
70
71
72
73
74
75
76
77
        return (
          <div>
            <h2>수고하셨습니다!</h2>
          </div>
        )
      }
      else {
        return (
          <div className="Quiz">
            <h2>Q:{this.state.Q}</h2>
baesangjune's avatar
ing    
baesangjune committed
78
79
80
81
82
83
84
            {this.state.Choose.map((a) =>
              <div>
                <input type="radio" name='answer' id={a} value={a} ref={this.textInput}/>
                <label for={a}>{a}</label>
                {this.answerbox()}

              </div>)}
baesangjune's avatar
Quiz.js    
baesangjune committed
85
86
87
88
89
90
            <div className="App">
              정답을 입력하세요
            </div>
            {/* <div>
              <input type="text" value={this.state.v} name="A" onChange={this.setAnswer} />
            </div> */}
baesangjune's avatar
ing    
baesangjune committed
91
            <button type="button" onClick={this.setQuestion}>다음</button>
baesangjune's avatar
Quiz.js    
baesangjune committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
          </div>
        )
      }
    }
    else {
      return (
        <div className="Box">
          <div className="Main">
            <h1> Calculus</h1>
          </div>
          <div className="Name">
            이름을 입력하세요
          <input onChange={(event) => { console.log(event.target.value) }} />
          </div>
          <button className="QuizStart" onClick={this.ShowQuiz}>Quiz Start !</button>
        </div>

      )
    }
  }
}

baesangjune's avatar
ing    
baesangjune committed
114
export default Quiz;