吸气返回undefined在JS,即使它在第一次调用
我有工作,当我使用它的哈巴狗第一次吸气,但后来它在我的代码后返回undefined(即使代码是完全一样的)。什么错?
问对象类和getter:
class Question {
constructor(op, min, max) {
this.op = op;
this.num1 = ranNums(min, max);
this.num2 = ranNums(min, max);
}
get operation() {
if (this.op === 'Addition') {
this.op = '+'
this.answer = this.num1 + this.num2;
return this.op;
} else if (this.op === 'Subtraction') {
this.op = '-'
this.answer = this.num1 - this.num2;
return this.op;
} else if (this.op === 'Multiplication') {
this.op = "*"
this.answer = this.num1 * this.num2;
return this.op;
} else {
this.op = '÷';
this.answer = this.num1 / this.num2;
return this.op;
}
}
在哈巴狗代码:(吸气剂首先在段,该段做什么,我想用然而,脚本标记后的代码没有。)。
html
head
p#question #{questions[score].num1} #{questions[score].operation} #{questions[score].num2}
//after script tag
if (input === questions[score].answer) {
score++
scoreElem.innerHTML = score;
qElem.innerHTML = `${questions[score].num1} ${questions[score].operation} ${questions[score].num2}`
}
这是我创建的问题阵列(在其他功能):
function createQuestions(op = 'Addition', min = 0, max = 20, amnt = 20) {
const questionArray = []
for (let i = 0; i < amnt; i++) {
questionArray.push(new Question(op, min, max));
}
return questionArray;
}
那么这是,如果我CONSOLE.LOG questions
和questions[score]
显示的内容:
[questions] =
0: {op: "+", num1: 6, num2: 1, answer: 7}
1: {op: "Addition", num1: 6, num2: 2}
2: {op: "Addition", num1: 0, num2: 8}
questions[score] = {op: "+", num1: 6, num2: 1, answer: 7}
answer: 7
num1: 6
num2: 1
op: "+"
//it has a + and answer property which the others don't have.
所以,当你第一次进入该页面,你得到的东西像10 + 40,如果你的问题是正确的,它给你的下一个数字对,但“+”变得不确定。
提前致谢!
回答如下:我能够做一个二传手,而不是当我创建的问题阵列使用它在我的for循环来解决这个问题。谢谢你们对你的想法!
吸气返回undefined在JS,即使它在第一次调用
我有工作,当我使用它的哈巴狗第一次吸气,但后来它在我的代码后返回undefined(即使代码是完全一样的)。什么错?
问对象类和getter:
class Question {
constructor(op, min, max) {
this.op = op;
this.num1 = ranNums(min, max);
this.num2 = ranNums(min, max);
}
get operation() {
if (this.op === 'Addition') {
this.op = '+'
this.answer = this.num1 + this.num2;
return this.op;
} else if (this.op === 'Subtraction') {
this.op = '-'
this.answer = this.num1 - this.num2;
return this.op;
} else if (this.op === 'Multiplication') {
this.op = "*"
this.answer = this.num1 * this.num2;
return this.op;
} else {
this.op = '÷';
this.answer = this.num1 / this.num2;
return this.op;
}
}
在哈巴狗代码:(吸气剂首先在段,该段做什么,我想用然而,脚本标记后的代码没有。)。
html
head
p#question #{questions[score].num1} #{questions[score].operation} #{questions[score].num2}
//after script tag
if (input === questions[score].answer) {
score++
scoreElem.innerHTML = score;
qElem.innerHTML = `${questions[score].num1} ${questions[score].operation} ${questions[score].num2}`
}
这是我创建的问题阵列(在其他功能):
function createQuestions(op = 'Addition', min = 0, max = 20, amnt = 20) {
const questionArray = []
for (let i = 0; i < amnt; i++) {
questionArray.push(new Question(op, min, max));
}
return questionArray;
}
那么这是,如果我CONSOLE.LOG questions
和questions[score]
显示的内容:
[questions] =
0: {op: "+", num1: 6, num2: 1, answer: 7}
1: {op: "Addition", num1: 6, num2: 2}
2: {op: "Addition", num1: 0, num2: 8}
questions[score] = {op: "+", num1: 6, num2: 1, answer: 7}
answer: 7
num1: 6
num2: 1
op: "+"
//it has a + and answer property which the others don't have.
所以,当你第一次进入该页面,你得到的东西像10 + 40,如果你的问题是正确的,它给你的下一个数字对,但“+”变得不确定。
提前致谢!
回答如下:我能够做一个二传手,而不是当我创建的问题阵列使用它在我的for循环来解决这个问题。谢谢你们对你的想法!