我有以下组成部分:
import React, { Component } from 'react';
import Utilities from './utilities.js';
import Action from './action.jsx';
import SingleGridEl from './singleGridEl.jsx';
class Grid extends Component {
constructor(props) {
super(props);
this.question;
this.maxN = 110;
this.array = [];
this.state = {
n1: false,
n2: false,
n3: false,
n4: false,
n5: false,
n6: false,
n7: false,
n8: false,
n9: false,
n10: false,
n11: false,
n12: false,
n13: false,
n14: false,
n15: false,
n16: false,
n17: false,
n18: false,
n19: false,
n20: false,
n21: false,
n22: false,
n23: false,
n24: false
}
this.hideSingleGrid = this.hideSingleGrid.bind(this);
}
getRandomQuestion() {
let randomN = Utilities.generateRandomNumber(this.array, this.maxN);
console.log(randomN);
this.question = this.props.current.data.questions[randomN];
console.log(this.question);
return this.question;
}
hideSingleGrid(el, val) {
var s = {};
s[el] = val;
this.setState(s);
}
componentWillMount() {
this.getRandomQuestion();
}
render() {
return (
<section className="game">
<div className="grid">
<div className="row">
<SingleGridEl visibility={this.state.n1} />
<SingleGridEl visibility={this.state.n2} />
<SingleGridEl visibility={this.state.n3} />
<SingleGridEl visibility={this.state.n4} />
<SingleGridEl visibility={this.state.n5} />
<SingleGridEl visibility={this.state.n6} />
</div>
<div className="row">
<SingleGridEl visibility={this.state.n7} />
<SingleGridEl visibility={this.state.n8} />
<SingleGridEl visibility={this.state.n9} />
<SingleGridEl visibility={this.state.n10} />
<SingleGridEl visibility={this.state.n11} />
<SingleGridEl visibility={this.state.n12} />
</div>
<div className="row">
<SingleGridEl visibility={this.state.n13} />
<SingleGridEl visibility={this.state.n14} />
<SingleGridEl visibility={this.state.n15} />
<SingleGridEl visibility={this.state.n16} />
<SingleGridEl visibility={this.state.n17} />
<SingleGridEl visibility={this.state.n18} />
</div>
<div className="row">
<SingleGridEl visibility={this.state.n19} />
<SingleGridEl visibility={this.state.n20} />
<SingleGridEl visibility={this.state.n21} />
<SingleGridEl visibility={this.state.n22} />
<SingleGridEl visibility={this.state.n23} />
<SingleGridEl visibility={this.state.n24} />
</div>
</div>
<Action t={this.hideSingleGrid} question={this.question} getRandomQuestion={this.getRandomQuestion.bind(this)}/>
</section>
);
}
}
export default Grid;
它利用了来自以下方面的实用程序方法:
var Utilities = (function (array, maxN) {
function generateRandomNumber(array, maxN) {
let randomN = Math.floor((Math.random() * maxN) + 1);
if(array.indexOf(randomN) === -1) {
array.push(randomN);
}
else {
randomN = generateRandomNumber();
}
console.log(array);
return randomN;
}
return {
generateRandomNumber: generateRandomNumber
};
})();
export default Utilities;
此实用程序方法生成一个随机数并将其存储在数组中,以便基于传入的最大长度,返回的每个随机数都是唯一的。
action.jsx组件使用相同的实用程序方法:
import React, { Component } from 'react';
import Utilities from './utilities.js';
class Action extends Component {
constructor(props) {
super(props);
this.maxN = 24;
this.array = [];
this.state = {
question: props.question
}
}
getNewQuestion(e) {
let answerId = "option_" + this.state.question.correct_option;
if(e.target.getAttribute('data-question') == answerId) {
let randomNumber = Utilities.generateRandomNumber(this.array, this.maxN);
this.props.t('n'+randomNumber, true);
this.setState({
question: this.props.getRandomQuestion()
});
}
else {
console.log('wrong');
}
}
render() {
let state = this.state;
return(
<div className="action">
<div className="action-question">
<h3>{state.question.question}</h3>
</div>
<div className="action-answers">
<p data-question="option_1" onClick={this.getNewQuestion.bind(this)}>{state.question.option_1}</p>
<p data-question="option_2" onClick={this.getNewQuestion.bind(this)}>{state.question.option_2}</p>
<p data-question="option_3" onClick={this.getNewQuestion.bind(this)}>{state.question.option_3}</p>
<p data-question="option_4" onClick={this.getNewQuestion.bind(this)}>{state.question.option_4}</p>
</div>
</div>
);
}
}
export default Action;
当我单击每个“ p”时,“ this.getNewQuestion”会检查我们是否单击了正确的答案,然后转到带有相对答案的下一个问题。
在游戏过程中的某个时刻(非常随机),我在控制台中收到以下错误。该错误涉及实用程序方法:
if(array.indexOf(randomN) === -1) {
我不知道是什么导致了该错误。
最佳答案
在调用array
之前,需要检查您的indexOf
参数是否实际上是一个数组:
if(Array.isArray(array) && array.indexOf(randomN) === -1) {
}
或提供默认参数
function generateRandomNumber(array = [], maxN) {
您代码中的这一行正在调用不带任何参数的函数,并且将触发错误:
else {
randomN = generateRandomNumber();
}