Quiz.js 3.12 KB
Newer Older
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
1
2
import React from 'react';
// import logo from './logo.svg';
Jiwon Yoon's avatar
push    
Jiwon Yoon committed
3
import './Quiz.css';
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
4
5
6


const question = [
Jiwon Yoon's avatar
d    
Jiwon Yoon committed
7
8
9
  { 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
10
11
12
13
14
]

class Quiz extends React.Component {
  constructor(props) {
    super(props)
Jiwon Yoon's avatar
d    
Jiwon Yoon committed
15
    // this.setAnswer = this.setAnswer.bind(this)
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
16
    this.setQuestion = this.setQuestion.bind(this)
Jiwon Yoon's avatar
d    
Jiwon Yoon committed
17
    this.ShowQuiz = this.ShowQuiz.bind(this)
baesangjune's avatar
ing    
baesangjune committed
18
    this.answerbox = this.answerbox.bind(this)
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
19
20
21
22
23
    this.state = {
      ...question[0],
      i: 0,
      page: 0,
    }
baesangjune's avatar
   
baesangjune committed
24
    // this.textInput = React.createRef()
baesangjune's avatar
ing    
baesangjune committed
25
        
Jiwon Yoon's avatar
Jiwon Yoon committed
26
  }
Jiwon Yoon's avatar
d    
Jiwon Yoon committed
27
  ShowQuiz() {
Jiwon Yoon's avatar
asdf    
Jiwon Yoon committed
28
    this.setState({ page: 1 })
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
29
30
  }
  setQuestion() {
baesangjune's avatar
   
baesangjune committed
31
    //값이 입력되지 않은채로 넘겨졌을 때 문제 해결 해야 함-sj-
baesangjune's avatar
ing    
baesangjune committed
32
33
    this.setState({ ...question[this.state.i + 1], i: this.state.i + 1 })

Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
34
  }
baesangjune's avatar
   
baesangjune committed
35
//answerbox - answer박스의 값을 네임리스트로 받아와서 값을 localstorage에 저장 
baesangjune's avatar
ing    
baesangjune committed
36
37
38
39
40
41
42
43
44
45
  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;
baesangjune's avatar
   
baesangjune committed
46
        localStorage.setItem('answer-'+i, checked_value)
baesangjune's avatar
ing    
baesangjune committed
47
48
49
50
51
52
53
54
55
56
57
58
59
      }
    }
    // 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
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)
  // }
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
66
  render() {
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
67
    if (this.state.page === 1) {
baesangjune's avatar
   
baesangjune committed
68
      this.answerbox()
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
69
      if (this.state.i === question.length) {
Jiwon Yoon's avatar
Jiwon Yoon committed
70
71
72
73
74
75
76
77
        return (
          <div>
            <h2>수고하셨습니다!</h2>
          </div>
        )
      }
      else {
        return (
Jiwon Yoon's avatar
push    
Jiwon Yoon committed
78
          <div className="Quiz">
Jiwon Yoon's avatar
Jiwon Yoon committed
79
            <h2>Q:{this.state.Q}</h2>
Jiwon Yoon's avatar
asdf    
Jiwon Yoon committed
80
81
            {this.state.Choose.map((a) =>
              <div>
baesangjune's avatar
   
baesangjune committed
82
                <input type="radio" name='answer' id={a} value={a} /*ref={this.textInput}*/ /> 
Jiwon Yoon's avatar
asdf    
Jiwon Yoon committed
83
                <label for={a}>{a}</label>
baesangjune's avatar
   
baesangjune committed
84
                
baesangjune's avatar
ing    
baesangjune committed
85

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

      )
    }
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
113
114
115
116
  }
}

export default Quiz;