import React from "react" import { View, StyleSheet, StatusBar, Text, SafeAreaView } from "react-native" import { Button, ButtonContainer } from "../components/Button" import { Alert } from "../components/Alert" const styles = StyleSheet.create({ container: { backgroundColor: "#36B1F0", flex: 1, paddingHorizontal: 20 }, text: { color: "#fff", fontSize: 25, textAlign: "center", letterSpacing: -0.02, fontWeight: "600" }, safearea: { flex: 1, marginTop: 100, justifyContent: "space-between" } }) class Quiz extends React.Component { state = { correctCount: 0, totalCount: this.props.navigation.getParam("questions", []).length, availableIds: this.props.navigation.getParam("questions", []).map(a => a.id), activeQuestionId: this.props.navigation.getParam("questions", [])[ Math.floor(Math.random() * this.props.navigation.getParam("questions", []).length) ].id, answered: false, answerCorrect: false } answer = correct => { this.setState( state => { const nextState = { answered: true } if (correct) { nextState.correctCount = state.correctCount + 1 nextState.answerCorrect = true } else { nextState.answerCorrect = false } return nextState }, () => { setTimeout(() => this.nextQuestion(), 750) } ) } nextQuestion = () => { this.setState(state => { const updatedIndexes = state.availableIds.filter( item => item != state.activeQuestionId) const nextId = updatedIndexes[Math.floor(Math.random() * updatedIndexes.length)] if (!updatedIndexes.length) { this.props.navigation.popToTop() } /* if (nextIndex >= state.totalCount) { this.props.navigation.popToTop() }*/ return { totalCount: updatedIndexes.length, availableIds: updatedIndexes, activeQuestionId: nextId, answered: false } }) } render() { const questions = this.props.navigation.getParam("questions", []) const question = questions.filter(item => item.id == this.state.activeQuestionId)[0] || questions[0] return ( {question.question} {question.answers.map(answer => (