【发布时间】:2019-06-07 20:50:44
【问题描述】:
所以我创建了一个测验应用程序,它会随机抽取 10 个问题,其中三个不正确和一个正确答案可供选择。目前一切都很好,除了我无法随机弹出答案。我的意思是,正确答案总是在所提供选项的底部。
我知道答案是 Math.floor(Math.random() * ... ) 但老实说我不知道该放在哪里。我什么都试过了。我真的需要一些帮助。
import React, { Component } from "react";
import "./App.css";
const API =
"https://opentdb.com/api.php?amount=10&category=20&difficulty=medium";
class App extends Component {
constructor(props) {
super(props);
this.state = {
results: [],
score: []
};
}
componentDidMount() {
this.populateAppWithData();
}
populateAppWithData() {
fetch(API)
.then(response => response.json())
.then(data => this.setState({ results: data.results }))
.catch(error => console.error(error))
}
render() {
const { results } = this.state;
return (
<div className="App">
<h1>Quiz App</h1>
<TheCounter results={results}
Counter={this.state.score}
/>
</div>
);
}
}
class MythologyAnswers extends Component {
constructor(props) {
super(props);
this.state = {
answered: "",
isRight: null,
};
}
answerClicked(answer) {
const { hasAnswered, correct_answer } = this.props;
return event => {
const isRight = correct_answer === answer;
hasAnswered(isRight);
this.setState({
answered: answer,
isRight,
});
};
}
render() {
const { question, correct_answer, incorrect_answers } = this.props;
const { answered, isRight } = this.state;
return (
<div className="allAnswers">
{question}
{incorrect_answers && incorrect_answers
.concat(correct_answer)
.map(answer => (<div onClick={this.answerClicked(answer)}>{answer} </div>))}<br />
{answered && `You answered ${answered}`} {" "} <br />
<div className="correctAnswer"> {" "}{answered && isRight && "This is correct!"} </div> <br />
<div className="incorrectAnswer"> {" "}{answered && !isRight && `This is incorrect. The correct answer is ${this.props.correct_answer}`} {" "}</div>
</div>
)
}
}
class TheCounter extends Component {
constructor(props) {
super(props);
this.state = {
right: 0,
Counter: 0,
unanswered: 0,
};
}
questionAnswered = isRight => {
this.setState(({ Counter, right }) => ({
Counter: Counter + 1,
right: right + isRight,
}));
}
render() {
const { results } = this.props;
const { Counter } = this.state;
const unanswered = this.props.results && Counter;
if (unanswered >= 10) {
return <div className="theUnanswered"> You got {this.state.right} right out of {this.state.Counter} </div>;
}
const question = results[Counter];
return (
<div className="newQuestion">
<MythologyAnswers {...question} hasAnswered={this.questionAnswered} />
</div>
)
}
}
export default App;
【问题讨论】:
-
嗨 Vicky,我刚刚为您提供了一个解决方案,说明如何在没有 Knufes 算法的情况下随机化您的数组。它还应该使您的标记更清晰:)
-
嗨@ChristopherNgo 非常感谢您的帮助和您的代码。我知道我的代码目前还不是很干净,但为了继续学习而不至于太困惑,我宁愿继续使用我已经拥有的代码。如果问的不是太多,您能否告诉我我可以用我已经拥有的东西来实现随机功能?当我第一次开始时,我将所有代码都放在一个组件中,我可以通过在调用结果状态时简单地添加 math.floor(math.random()) 来随机化,但是自从我将它分成三部分后,我就没有了线索在哪里做。
-
让我看看是否可以修改我的答案以使用您现有的代码。同时,你可以试试我下面的答案,看看它是否有效?
-
嗨,Vicky,整合以下内容有什么运气吗?我仍在修改代码,使其看起来与您编写的内容没有太大差异。
-
Vicky,嗯,我不太确定。我猜想这与这条线有关:
{answered && !isRight && This is incorrect. The correct answer is ${ this.props.correct_answer }}你从使用 MythologyAnswers 的组件中作为名为 correct_answer 的道具传入了什么?