Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
students
online-quiz-react
Commits
f48758b3
Commit
f48758b3
authored
Aug 05, 2020
by
baesangjune
Browse files
0805 final
parent
85e2daf5
Changes
2
Show whitespace changes
Inline
Side-by-side
src/Quiz.js
View file @
f48758b3
import
React
,
{
useState
}
from
'
react
'
import
React
,
{
useState
}
from
'
react
'
import
{
Link
}
from
'
react-router-dom
'
;
import
{
Link
}
from
'
react-router-dom
'
;
import
Timer
from
'
react-compound-timer
'
;
// 타이머쓰기위해 import
import
Timer
from
'
react-compound-timer
'
;
// 타이머쓰기위해 import
import
logo
from
'
./img_question.png
'
import
logo
from
'
./img/img_question.png
'
import
fight
from
'
./img_quiz.png
'
import
'
./Quiz.css
'
const
QnA
=
[
const
QnA
=
[
{
Q
:
"
6 X 4 = ?
"
,
Choose
:
[
6
,
12
,
18
,
24
],
A
:
"
4
"
,
N
:
1
},
{
Q
:
"
6 X 4 = ?
"
,
Choose
:
[
6
,
12
,
18
,
24
],
A
:
"
4
"
,
N
:
1
},
...
@@ -15,67 +16,59 @@ let Solutions = [4, 3, 2]
...
@@ -15,67 +16,59 @@ let Solutions = [4, 3, 2]
localStorage
.
setItem
(
'
Solutions
'
,
JSON
.
stringify
(
Solutions
))
localStorage
.
setItem
(
'
Solutions
'
,
JSON
.
stringify
(
Solutions
))
function
Quiz
()
{
function
Quiz
()
{
le
t
[
question
,
setQuestion
]
=
useState
({
cons
t
[
question
,
setQuestion
]
=
useState
({
...
QnA
[
0
],
...
QnA
[
0
],
i
:
0
,
i
:
0
,
page
:
0
,
page
:
0
,
})
})
let
[
selected
,
setSelected
]
=
useState
(
""
)
//선택한 답을 보여줄 것들
const
[
selected
,
setSelected
]
=
useState
(
""
)
//선택한 답을 보여줄 것들
// const [checked, setChecked] = useState(false)
function
handleQuestion
()
{
function
handleQuestion
()
{
setQuestion
({
...
QnA
[
question
.
i
+
1
],
i
:
question
.
i
+
1
,
page
:
question
.
page
+
1
})
setQuestion
({
...
QnA
[
question
.
i
+
1
],
i
:
question
.
i
+
1
,
page
:
question
.
page
+
1
})
// setChecked(false)
}
}
let
handleChange
=
(
ev
)
=>
{
let
handleChange
=
(
ev
)
=>
{
ev
.
preventDefault
()
//새로고침 안되도록
//
ev.preventDefault() //새로고침 안되도록
setSelected
(
ev
.
target
.
value
)
//selected값 변경
setSelected
(
ev
.
target
.
value
)
//selected값 변경
Answers
[
question
.
N
-
1
]
=
Number
(
ev
.
target
.
id
)
+
1
Answers
[
question
.
N
-
1
]
=
Number
(
ev
.
target
.
id
)
+
1
localStorage
.
setItem
(
'
Answers
'
,
JSON
.
stringify
(
Answers
))
localStorage
.
setItem
(
'
Answers
'
,
JSON
.
stringify
(
Answers
))
}
}
return
(
return
(
<>
<
div
className
=
"
container-fluid position-absolute
"
>
<
div
style
=
{{
fontSize
:
'
80px
'
,
marginBottom
:
'
100px
'
,
textAlign
:
"
center
"
,
backgroundColor
:
'
red
'
}}
>
미적분학
퀴즈
<
/div
>
<
div
className
=
"
text-center h2 font-weight-bold bg-warning py-2
"
>
미적분학
퀴즈
<
/div
>
<
img
src
=
{
fight
}
style
=
{{
position
:
"
absolute
"
,
left
:
"
1050px
"
,
top
:
'
200px
'
}}
alt
=
"
lion
"
/>
<
div
className
=
"
row justify-content-md-center
"
>
<
div
className
=
"
Quiz
"
>
<
div
className
=
"
col text-right
"
>
<
h1
><
img
src
=
{
logo
}
style
=
{{
marginLeft
:
"
450px
"
}}
width
=
'
75
'
height
=
'
75
'
alt
=
'
question
'
/>
<
span
style
=
{{
fontSize
:
"
75px
"
,
marginLeft
:
"
30px
"
}}
>
{
question
.
Q
}
<
/span></
h1
>
<
h1
>
{
/* <div style={{ marginTop: "30px", marginBottom: "30px", marginLeft:'450px', fontSize:'40px' }}>정답을 선택하세요</div> */
}
<
img
src
=
{
logo
}
width
=
'
50
'
height
=
'
50
'
alt
=
'
question
'
/>
<
/h1
>
<
div
>
<
/div
>
<
div
className
=
"
col-md-auto
"
>
<
div
className
=
"
h2 mt-2
"
>
{
question
.
Q
}
<
/div
>
<
div
className
=
"
mt-2
"
>
<
form
>
<
form
>
{
question
.
Choose
.
map
((
a
,
index
)
=>
{
question
.
Choose
.
map
((
a
,
index
)
=>
<
div
key
=
{
index
}
>
<
div
key
=
{
index
}
>
<
input
type
=
"
radio
"
name
=
'
answer
'
id
=
{
index
}
value
=
{
a
}
onClick
=
{
handleChange
}
style
=
{{
marginLeft
:
"
475px
"
,
width
:
"
25px
"
,
height
:
"
25px
"
}}
/
>
<
input
type
=
"
radio
"
name
=
'
answer
'
id
=
{
index
}
value
=
{
a
}
onClick
=
{
handleChange
}
/
>
<
label
htmlFor
=
{
a
}
style
=
{{
fontSize
:
"
40px
"
,
marginLeft
:
"
22px
"
}
}
>
{
a
}
<
/label
>
<
label
className
=
"
font-weight-bold
"
htmlFor
=
{
a
}
>
{
a
}
<
/label
>
<
/div
>
<
/div
>
)}
)}
<
input
hidden
type
=
"
submit
"
value
=
"
확인
"
/>
{
/*버튼 숨김*/
}
<
input
hidden
type
=
"
submit
"
value
=
"
확인
"
/>
{
/*버튼 숨김*/
}
<
/form
>
<
/form
>
<
p
style
=
{{
fontSize
:
"
40px
"
,
marginLeft
:
"
480px
"
}}
>
Answer
:
<
span
style
=
{{
color
:
"
green
"
,
fontWeight
:
"
bold
"
,
fontSize
:
"
90px
"
,
marginLeft
:
"
30px
"
}}
>
{
selected
}
<
/span> {/
*
선택한
값
보여줌
*
/}<span style={{ marginLeft: "50px" }}
>
<
span
className
=
"
h5 font-weight-bold
"
>
Your
Answer
:
<
/span
>
{(
question
.
page
===
QnA
.
length
-
1
)
<
span
className
=
"
h2 font-weight-bold text-danger
"
>
{
selected
}
<
/span> {/
*
선택한
값
보여줌
*
/
}
?
<
Link
to
=
"
/end
"
>
제출
<
/Link
>
<
div
className
=
"
text-center my-3
"
>
{(
question
.
page
===
QnA
.
length
-
1
)
:
<
button
type
=
"
button
"
onClick
=
{
handleQuestion
}
style
=
{{
width
:
"
85px
"
,
height
:
"
40px
"
}}
>
다음
<
/button
>
?
<
Link
to
=
"
/end
"
>
<
button
className
=
"
btn btn-outline-success
"
>
제출
<
/button
>
}
<
/span
>
<
/Link
>
<
/p
>
:
<
button
type
=
"
button
"
className
=
"
btn btn-outline-dark
"
onClick
=
{
handleQuestion
}
>
다음
<
/button
>
}
<
/div
>
<
/div
>
<
p
className
=
"
h3 text-center text-danger
"
>
{
/* 마지막 질문일 경우 /end페이지로 이동, 그렇지 않을경우는 this.handleQuestion발생 */
}
{
/* {(question.page === QnA.length - 1)
? <Link to="/end">제출</Link>
: <button type="button" onClick={handleQuestion} >다음</button>
} */
}
{
/* <input onKeyPress="this.enterkey()"/> */
}
<
p
style
=
{{
textAlign
:
"
center
"
,
fontSize
:
"
30px
"
,
color
:
"
crimson
"
}}
>
<
Timer
<
Timer
initialTime
=
{
3600000
}
initialTime
=
{
3600000
}
direction
=
"
backward
"
direction
=
"
backward
"
...
@@ -93,9 +86,12 @@ function Quiz() {
...
@@ -93,9 +86,12 @@ function Quiz() {
)}
)}
<
/Timer> {/
*
npm
i
react
-
compound
-
timer
*
/
}
<
/Timer> {/
*
npm
i
react
-
compound
-
timer
*
/
}
<
/p
>
<
/p
>
<
/div
>
<
/div
>
<
/
>
<
/div
>
<
div
className
=
"
col
"
>
<
/div
>
<
/div
>
<
/div
>
)
)
...
...
src/preQuiz.js
View file @
f48758b3
import
React
from
'
react
'
;
import
React
,
{
useState
}
from
'
react
'
// import logo from './logo.svg';
import
{
Link
}
from
'
react-router-dom
'
;
import
'
./Quiz.css
'
;
import
Timer
from
'
react-compound-timer
'
;
// 타이머쓰기위해 import
import
logo
from
'
./img/img_question.png
'
import
'
./Quiz.css
'
const
question
=
[
const
QnA
=
[
{
Q
:
"
6 X 4 ?
"
,
Choose
:
[
6
,
12
,
18
,
24
],
A
:
"
"
},
{
Q
:
"
6 X 4
=
?
"
,
Choose
:
[
6
,
12
,
18
,
24
],
A
:
"
4
"
,
N
:
1
},
{
Q
:
"
3 + 3 ?
"
,
Choose
:
[
2
,
4
,
6
,
8
],
A
:
"
"
},
{
Q
:
"
3 + 3
=
?
"
,
Choose
:
[
2
,
4
,
6
,
8
],
A
:
"
3
"
,
N
:
2
},
{
Q
:
"
3 - 1 ?
"
,
Choose
:
[
1
,
2
,
3
,
4
],
A
:
"
"
}
{
Q
:
"
3 - 1
=
?
"
,
Choose
:
[
1
,
2
,
3
,
4
],
A
:
"
2
"
,
N
:
3
}
]
]
class
Quiz
extends
React
.
Component
{
let
Answers
=
[]
constructor
(
props
)
{
let
Solutions
=
[
4
,
3
,
2
]
super
(
props
)
localStorage
.
setItem
(
'
Solutions
'
,
JSON
.
stringify
(
Solutions
))
// this.setAnswer = this.setAnswer.bind(this)
this
.
setQuestion
=
this
.
setQuestion
.
bind
(
this
)
function
Quiz
()
{
this
.
ShowQuiz
=
this
.
ShowQuiz
.
bind
(
this
)
const
[
question
,
setQuestion
]
=
useState
({
this
.
answerbox
=
this
.
answerbox
.
bind
(
this
)
...
QnA
[
0
],
this
.
state
=
{
...
question
[
0
],
i
:
0
,
i
:
0
,
page
:
0
,
page
:
0
,
}
})
// this.textInput = React.createRef()
const
[
selected
,
setSelected
]
=
useState
(
""
)
//선택한 답을 보여줄 것들
// const [checked, setChecked] = useState(false)
function
handleQuestion
()
{
setQuestion
({
...
QnA
[
question
.
i
+
1
],
i
:
question
.
i
+
1
,
page
:
question
.
page
+
1
})
// setChecked(false)
}
}
ShowQuiz
()
{
this
.
setState
({
page
:
1
})
}
setQuestion
()
{
//값이 입력되지 않은채로 넘겨졌을 때 문제 해결 해야 함-sj-
this
.
setState
({
...
question
[
this
.
state
.
i
+
1
],
i
:
this
.
state
.
i
+
1
})
}
let
handleChange
=
(
ev
)
=>
{
//answerbox - answer박스의 값을 네임리스트로 받아와서 값을 localstorage에 저장
// ev.preventDefault() //새로고침 안되도록
answerbox
()
{
setSelected
(
ev
.
target
.
value
)
//selected값 변경
let
answers
=
document
.
getElementsByName
(
'
answer
'
);
Answers
[
question
.
N
-
1
]
=
Number
(
ev
.
target
.
id
)
+
1
let
count
=
answers
.
length
// var checked_index = -1;
var
checked_value
=
''
;
for
(
var
i
=
0
;
i
<
count
;
i
++
)
{
localStorage
.
setItem
(
'
Answers
'
,
JSON
.
stringify
(
Answers
))
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);
// }
}
// setAnswer(e) {
// question[this.state.i]["A"] = e.target.value
// this.setState({ v: e.target.value })
// console.log(this.state)
// console.log(question)
// }
render
()
{
if
(
this
.
state
.
page
===
1
)
{
this
.
answerbox
()
if
(
this
.
state
.
i
===
question
.
length
)
{
return
(
return
(
<
div
>
<
div
className
=
"
container-fluid position-absolute
"
>
<
h2
>
수고하셨습니다
!<
/h2
>
<
div
className
=
"
text-center h2 font-weight-bold bg-warning py-2
"
>
미적분학
퀴즈
<
/div
>
<
div
className
=
"
row justify-content-md-center
"
>
<
div
className
=
"
col text-right
"
>
<
h1
>
<
img
src
=
{
logo
}
width
=
'
50
'
height
=
'
50
'
alt
=
'
question
'
/>
<
/h1
>
<
/div
>
<
/div
>
)
<
div
className
=
"
col-md-auto
"
>
}
<
div
className
=
"
h2 mt-2
"
>
else
{
{
question
.
Q
}
return
(
<
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
>
<
/div
>
{
/* <div>
<
div
className
=
"
mt-2
"
>
<input type="text" value={this.state.v} name="A" onChange={this.setAnswer} />
<
form
>
</div> */
}
{
question
.
Choose
.
map
((
a
,
index
)
=>
<
button
type
=
"
button
"
onClick
=
{
setTimeout
(
this
.
setQuestion
,
2000
)}
>
다음
<
/button
>
<
div
key
=
{
index
}
>
<
input
type
=
"
radio
"
name
=
'
answer
'
id
=
{
index
}
value
=
{
a
}
onClick
=
{
handleChange
}
/
>
<
label
className
=
"
font-weight-bold
"
htmlFor
=
{
a
}
>
{
a
}
<
/label
>
<
/div
>
<
/div
>
)
)}
<
input
hidden
type
=
"
submit
"
value
=
"
확인
"
/>
{
/*버튼 숨김*/
}
<
/form
>
<
span
className
=
"
h5 font-weight-bold
"
>
Your
Answer
:
<
/span
>
<
span
className
=
"
h2 font-weight-bold text-danger
"
>
{
selected
}
<
/span> {/
*
선택한
값
보여줌
*
/
}
<
div
className
=
"
text-center my-3
"
>
{(
question
.
page
===
QnA
.
length
-
1
)
?
<
Link
to
=
"
/end
"
>
<
button
className
=
"
btn btn-outline-success
"
>
제출
<
/button
>
<
/Link
>
:
<
button
type
=
"
button
"
className
=
"
btn btn-outline-dark
"
onClick
=
{
handleQuestion
}
>
다음
<
/button
>
}
}
<
/div
>
<
p
className
=
"
h3 text-center text-danger
"
>
<
Timer
initialTime
=
{
3600000
}
direction
=
"
backward
"
checkpoints
=
{[
{
time
:
0
,
callback
:
<
Link
to
=
"
/end
"
>
제출
<
/Link
>
// history.go(1)
}
}
else
{
]}
return
(
>
<
div
className
=
"
Box
"
>
{()
=>
(
<
div
className
=
"
Main
"
>
<>
<
h1
>
Calculus
<
/h1
>
<
Timer
.
Minutes
/>
:
<
Timer
.
Seconds
><
/Timer.Seconds> /
60
:
00
<
/
>
)}
<
/Timer> {/
*
npm
i
react
-
compound
-
timer
*
/
}
<
/p
>
<
/div
>
<
/div
>
<
div
className
=
"
col
"
>
<
/div
>
<
/div
>
<
div
className
=
"
Name
"
>
이름을
입력하세요
<
input
onChange
=
{(
event
)
=>
{
console
.
log
(
event
.
target
.
value
)
}}
/
>
<
/div
>
<
/div
>
<
button
className
=
"
QuizStart
"
onClick
=
{
this
.
ShowQuiz
}
>
Quiz
Start
!<
/button
>
<
/div
>
<
/div
>
)
)
}
}
}
}
export
default
Quiz
;
export
default
Quiz
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment