-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrivia.js
133 lines (120 loc) · 3.41 KB
/
Trivia.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
import React, { Component } from 'react';
import { Text, View, StyleSheet, TouchableHighlight, ScrollView } from 'react-native';
import { generateTrivia } from './QuestionGenerator';
import Icon from 'react-native-vector-icons/FontAwesome';
class Trivia extends Component {
static navigationOptions = {
title: 'US Flag Trivia',
headerStyle: {
backgroundColor: '#4169e1',
height: 40,
},
headerForceInset: { top: 'never', bottom: 'never' },
headerTintColor: '#FFFFFF',
};
constructor(props) {
super(props);
this.state = { needsReset: false, trivia: generateTrivia(), selected: null };
}
_reset = () => {
this.setState({ needsReset: false, trivia: generateTrivia(), selected: null });
}
_answerSelected = (answer) => {
this.setState({ needsReset: true, selected: answer });
}
// fisher-yates shuffle
_shuffle(array) {
let counter = array.length;
while (counter > 0) {
let index = Math.floor(Math.random() * counter);
counter--;
let temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
_getTriviaComponent() {
const trivia = this.state.trivia;
const shuffledAnswers = this._shuffle([...trivia.answers]);
return (
<View>
<Text style={styles.question}>{trivia.question}</Text>
{shuffledAnswers.map((answer, i) => (
<View style={styles.answerContainer} key={i}>
<TouchableHighlight onPress={() => this._answerSelected(answer)} underlayColor="#87ceeb">
<Text style={styles.answer}>{answer}</Text>
</TouchableHighlight>
</View>
))
}
</View>);
}
_getResultComponent = () => {
const isCorrect = this.state.selected == this.state.trivia.answers[this.state.trivia.answerIndex];
return (
<TouchableHighlight onPress={() => this._reset()} underlayColor="#87ceeb">
<View>
<Text style={styles.answer}>{isCorrect ? 'Correct!' : 'Wrong!'}</Text>
<Text style={styles.question}>{this.state.trivia.followup}</Text>
<View style={styles.arrowContainer}>
<Icon name="arrow-right" size={25} style={styles.chevron} />
</View>
</View>
</TouchableHighlight>);
}
render() {
return (
<View style={styles.outer}>
<View style={styles.page}>
<ScrollView>
{ !this.state.needsReset ? this._getTriviaComponent() : null}
{ this.state.needsReset ? this._getResultComponent() : null}
</ScrollView>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
outer: {
flex: 1,
backgroundColor: '#d3d3d3',
justifyContent: 'center',
},
page: {
backgroundColor: 'white',
marginLeft: 10,
marginRight: 10,
padding: 15,
borderRadius: 3,
borderColor: '#4169e1',
borderWidth: 4,
},
arrowContainer: {
alignItems: 'flex-end',
alignSelf: 'stretch',
},
question: {
fontSize: 20,
fontWeight: '800',
paddingTop: 12,
paddingBottom: 12,
//fontFamily: 'sans-serif-condensed',
color: 'crimson',
},
answerContainer: {
borderTopColor: 'black',
borderWidth: 1,
borderRadius: 8,
marginTop: 8,
alignSelf: 'stretch',
},
answer: {
fontSize: 14,
fontWeight: '800',
padding: 8,
//fontFamily: 'sans-serif-condensed',
},
});
export default Trivia;