当我在pug中使用它时,我有一个可以工作的吸气剂,但是后来在我的代码中它返回undefined(即使代码完全相同)。怎么了
问题对象类和获取器:
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的信息。如果您正确地回答了该问题,它会为您提供下一对数字,但是“ +”变得不确定。
谢谢!
答案 0 :(得分:0)
我能够通过创建一个setter并在创建问题数组时在for循环中使用它来解决此问题。谢谢你们的想法!