|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import './App.css'; |
| 3 | +import Question from './Components/question'; |
| 4 | + |
| 5 | +class App extends Component { |
| 6 | + constructor(props){ |
| 7 | + super(props) |
| 8 | + this.state={ |
| 9 | + questionId:111, |
| 10 | + questions:[ |
| 11 | + { |
| 12 | + id:1, |
| 13 | + questionText:"Who is Primeminister of Bangladesh", |
| 14 | + choices:['Shekh hasina','Jillu Rahman','Ziaur Rahman','Khaleda Zia'], |
| 15 | + answer:['Shekh hasina'], |
| 16 | + isAnswer:false,isWrong:false |
| 17 | + }, |
| 18 | + { |
| 19 | + id:2, |
| 20 | + questionText:"Who is Primeminister of woganda", |
| 21 | + choices:['chan chan lue','Jillu Rahman','Ziaur Rahman','Khaleda Zia'], |
| 22 | + answer:['chan chan lue'], |
| 23 | + isAnswer:false, |
| 24 | + isRight:false |
| 25 | + }, |
| 26 | + { |
| 27 | + id:3, |
| 28 | + questionText:"বাংলাদেশের কতগুলো জেলা আছে ? ", |
| 29 | + choices:['৬৭ টি','৬৬ টি','৬৪ টি','৬২ টি'], |
| 30 | + answer:['৬৪ টি'], |
| 31 | + isAnswer:false,isWrong:false |
| 32 | + }, |
| 33 | + ], |
| 34 | + isStart:false,point:'', |
| 35 | + } |
| 36 | + } |
| 37 | + ///methods |
| 38 | + chackChangedHandler=(event,id,answer)=>{ |
| 39 | + const fIndex=this.state.questions.findIndex(p=>{ |
| 40 | + return p.id===id; |
| 41 | + }) |
| 42 | + |
| 43 | + if(event.target.value===answer){ |
| 44 | + const qu={ |
| 45 | + ...this.state.questions[fIndex] |
| 46 | + } |
| 47 | + qu.isAnswer=true; |
| 48 | + qu.isRight=true; |
| 49 | + const ques=[...this.state.questions] |
| 50 | + ques[fIndex]=qu; |
| 51 | + console.log(qu.isAnswer); |
| 52 | + this.setState({ |
| 53 | + questions:ques |
| 54 | + }) |
| 55 | + }else{ |
| 56 | + const qu={ |
| 57 | + ...this.state.questions[fIndex] |
| 58 | + } |
| 59 | + qu.isAnswer=true; |
| 60 | + qu.isRight=false; |
| 61 | + const ques=[...this.state.questions] |
| 62 | + ques[fIndex]=qu; |
| 63 | + // console.log(qu.isAnswer); |
| 64 | + this.setState({ |
| 65 | + questions:ques |
| 66 | + }) |
| 67 | + } |
| 68 | + // console.log(this.state.questions[fIndex].isAnswer); |
| 69 | + } |
| 70 | + startExam=()=>{ |
| 71 | + const st=this.state.isStart; |
| 72 | + this.setState({ |
| 73 | + isStart:!st |
| 74 | + }) |
| 75 | + } |
| 76 | + endExam=(qId)=>{ |
| 77 | + if(this.state.questionId===qId){ |
| 78 | + console.log('why this is fired?'); |
| 79 | + } |
| 80 | + } |
| 81 | +///methods end |
| 82 | + render() { |
| 83 | + let output=null,btnoutput=null; |
| 84 | + if(!this.state.isStart){ |
| 85 | + btnoutput=( |
| 86 | + <div> |
| 87 | + <button |
| 88 | + className="btn btn-outline-success" |
| 89 | + style={{float:'',margin:'9px',minHeight:''}} |
| 90 | + onClick={this.startExam} |
| 91 | + >Start</button> |
| 92 | + </div> |
| 93 | + ) |
| 94 | + } |
| 95 | + if(this.state.isStart){ |
| 96 | + output=( |
| 97 | + <div> |
| 98 | + {this.state.questions.map(q=>{ |
| 99 | + return <Question |
| 100 | + key={q.id} |
| 101 | + qId={q.id} |
| 102 | + qText={q.questionText} |
| 103 | + qChoices={q.choices} |
| 104 | + qIsAnswer={q.isAnswer} |
| 105 | + qIsRight={q.isRight} |
| 106 | + chackChanged={event=>this.chackChangedHandler(event,q.id,q.answer[0])} |
| 107 | + /> |
| 108 | + })} |
| 109 | + <div> |
| 110 | + <button |
| 111 | + onClick={()=>this.endExam(this.state.questionId)} |
| 112 | + className="btn btn-danger" |
| 113 | + >End Exam</button> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + return ( |
| 120 | + <div className="App container list-group-out"> |
| 121 | + <div> |
| 122 | + <h5 style={{float:'left',padding:'5px',minHeight:'70px',margin:'9px'}}>Exam Name : MCQ</h5> |
| 123 | + {btnoutput} |
| 124 | + {output} |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +export default App; |
| 132 | + |
| 133 | + |
0 commit comments