我的程序应该是一个测验,用户需要回答问题。回答后,它会向用户显示一个摘要。然后提示用户0=提交。1=重做问题。
错误是这样的。如果用户选择重做问题。重做后,会显示摘要,但不再提示提交。
导致提交函数 = 不是函数
function showquestion() {
for (i = 0; i < 5; i++) {
console.log("\n" + "Question" + "(" + (i + 1) + ") " + quiz.mcqSelected[i].getQuestion() + "\n")
useranswer = parseInt(input.question("Please select your answer ")) + "\n"
answerstorage.push(useranswer);
console.log("You have selected answer:" + quiz.questionPool[i].choices[(parseInt(answerstorage[i])) - 1])
// break
}
showsummary();
}
function showsummary() {
console.log("Here are your answers:\n")
for (i = 0; i < 5; i++) {
console.log("\n" + "Question" + "(" + (i + 1) + ") " + quiz.mcqSelected[i].getQuestion() + "\n")
console.log("Answer: " + quiz.questionPool[i].choices[(parseInt(answerstorage[i])) - 1])
}
submit();
}
function submit() {
submit = parseInt(input.question("Enter 0 to submit your quiz, 1 to change your answer or any other keys to reselect category"));
if (submit == 0) {
console.log("You have submited your quiz.")
} else (submit == 1) {showquestion()}
}
您正在submit
内部重新分配函数,因此如果您打算再次调用它,它将不起作用。你基本上是这样做的:
function foo() {
foo = "I'm a string now";
}
foo();
console.log( typeof foo );
console.log( foo );
foo();
请注意foo
,在调用该函数一次之后,类型现在是一个字符串,因为我重新分配了foo
at foo = "foo"
。
您可以为 的结果使用不同的变量input.question("Enter 0 to submit your quiz, ...
,例如:
function submit() {
const answer = parseInt(input.question("Enter 0 to submit your quiz, 1 to change your answer or any other keys to reselect category"));
if (answer == 0) {
console.log("You have submited your quiz.")
} else (answer == 1) {showquestion()}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句