-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQuiz.js
224 lines (207 loc) · 6.06 KB
/
Quiz.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import React, { useState, useEffect} from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
import App from './App';
const questions = [
{
question: 'What does "Phishing" refer to in cybersecurity?',
options: [
'A technique to gain unauthorized access to a system',
'A fraudulent attempt to obtain sensitive information by disguising as a trustworthy entity',
'A type of malware that encrypts files',
],
correctAnswer: 1,
},
{
question: 'Which of the following is a strong password?',
options: ['"password123"', '"P@ssw0rd!"', '"qwerty"'],
correctAnswer: 1,
},
{
question: 'What is the purpose of using a Virtual Private Network (VPN)?',
options: [
'To secure wireless network connections',
'To hide your IP address and encrypt your data while browsing',
'To protect against malware attacks',
],
correctAnswer: 1,
},
{
question: 'Why is it important to regularly update your software and applications?',
options: [
'To make your device run faster',
'To add new features',
'To patch security vulnerabilities and protect against cyber threats',
],
correctAnswer: 2,
},
{
question: 'If you receive an email requesting your login credentials or personal information, what should you do?',
options: [
'Provide the requested information to verify your account',
'Delete the email without responding or clicking on any links',
'Reply to the email and ask for more information',
],
correctAnswer: 1,
},
];
const Quiz = () => {
const [currentQuestionIndex, setCurrentQuestionIndex] = useState(0);
const [userScore, setUserScore] = useState(0);
const [showNextButton, setShowNextButton] = useState(false);
const [showScore, setShowScore] = useState(false);
const [selectedOption, setSelectedOption] = useState(null);
const [isAnswerCorrect, setIsAnswerCorrect] = useState(false);
const [showApp, setApp] = useState(false);
const handleNextQuestion = () => {
if (currentQuestionIndex < questions.length - 1) {
setCurrentQuestionIndex(currentQuestionIndex + 1);
setShowNextButton(false);
setSelectedOption(null);
setIsAnswerCorrect(false);
} else {
setShowScore(true);
}
};
const handleAnswer = (index) => {
if (!selectedOption) {
setSelectedOption(index);
const correctAnswer = questions[currentQuestionIndex].correctAnswer;
setIsAnswerCorrect(index === correctAnswer);
if (index === correctAnswer) {
setUserScore(userScore + 1);
}
setShowNextButton(true);
}
};
useEffect(() => {
setShowNextButton(false);
}, [currentQuestionIndex]);
if (showApp) {
return <App />;
}
return (
<View style={styles.container}>
{showScore ? (
<View style={styles.scoreContainer}>
<Text style={styles.scoreText}>Your Score: {userScore} / {questions.length}</Text>
<View style={styles.buttonBox}><TouchableOpacity style={styles.button} onPress={() => setApp(true)}>
<Text style={styles.buttonText}>Home</Text>
</TouchableOpacity></View>
</View>
) : (
<View>
<Text style={styles.questionText}>{questions[currentQuestionIndex].question}</Text>
{questions[currentQuestionIndex].options.map((option, index) => (
<TouchableOpacity
key={index}
onPress={() => handleAnswer(index)}
style={[
styles.optionButton,
{
backgroundColor:
selectedOption === index
? isAnswerCorrect
? 'green'
: 'red'
: '#f0f0f0',
},
]}
disabled={selectedOption !== null}
>
<Text
style={[
styles.optionText,
{ color: selectedOption === index ? 'white' : 'black' },
]}
>
{option}
</Text>
</TouchableOpacity>
))}
</View>
)}
{showNextButton && !showScore && (
<TouchableOpacity onPress={handleNextQuestion} style={styles.nextButton}>
<Text style={styles.nextButtonText}>
{currentQuestionIndex < questions.length - 1 ? 'Next' : 'Finish!'}
</Text>
</TouchableOpacity>
)}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 20,
backgroundColor: '#0066A4',
},
scoreContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
scoreText: {
fontSize: 24,
fontWeight: 'bold',
color: '#ffffff'
},
questionText: {
fontSize: 24,
marginBottom: 20,
textAlign: 'justify',
color: '#ffffff'
},
optionButton: {
padding: 10,
borderRadius: 10,
marginBottom: 20,
paddingTop: 10,
height: 100,
justifyContent: 'center'
},
optionText: {
fontSize: 18,
textAlign: 'center',
alignItems: 'center'
},
nextButton: {
backgroundColor: '#3498db',
padding: 15,
borderRadius: 10,
marginTop: 20,
},
nextButtonText: {
fontSize: 18,
color: 'white',
textAlign: 'center',
},
button: {
width: 100,
height: 45,
borderRadius: 10,
backgroundColor: '#FFF',
textAlign: 'center',
paddingTop: 10,
paddingBottom: 10,
marginTop: 30,
alignItems: 'center',
},
buttonText: {
color: '#0066A4',
textAlign: 'center',
fontSize: 20,
},
buttonBox: {
marginBottom: 0,
justifyContent: 'center',
alignItems: 'center',
width: '100%',
fontFamily: 'bell-slim',
padding: 15,
marginTop:200
},
});
export default Quiz;