繁体   English   中英

为什么我的变量在 javascript 中返回 undefined?

[英]Why is my variable returning undefined in javascript?

我是 javascript 新手。 我正在制作一个抽认卡程序。 出于某种原因,尽管将变量 lastPress 定义为全局变量,但 lastPress 似乎返回 undefined。 但是,它不会导致 JSHint 中的错误。 这是我的代码:

 var i = 0; var questions = ["1: Was the human migration caused by an ice age?", "True"]; var answer = [true, true]; var lastPress; document.getElementById("question").innerHTML = questions[i]; function itsTrue() { lastPress = true; } function itsFalse() { lastPress = false; } function getAns() { if (answer[i] == lastPress) { document.getElementById("ans").innerHTML = "You're correct! The answer was " + lastPress; } else { document.getElementById("ans").innerHTML = "We're sorry, but that is not correct. You said " + lastPress + ". The answer was " + answer[i]; } i++; document.getElementById("question").innerHTML = questions[i]; } document.getElementById("true").onclick = function(){itsTrue()}; document.getElementById("true").onclick = function(){getAns()}; document.getElementById("false").onclick = function(){itsFalse()}; document.getElementById("false").onclick = function(){getAns()};
 <!DOCTYPE HTML> <html> <head> <title>Flash cards</title> </head> <body> <p id="question">This is where the question shows up</p> <p id="ans">This is where the result will show up</p> <button type="button" id="true">True</button> <button type="button" id="false">False</button> </body> </html>

如果您想为同一个点击事件定义多个函数:此外,在第二次按下真假按钮后,我将增加到 2,因此问题 [2] 未定义

 var i = 0; var questions = ["1: Was the human migration caused by an ice age?", "True"]; var answer = [true, true]; var lastPress; document.getElementById("question").innerHTML = questions[i]; function itsTrue() { console.log('true') lastPress = true; } function itsFalse() { console.log('false'); lastPress = false; } function getAns() { if (answer[i] == lastPress) { document.getElementById("ans").innerHTML = "You're correct! The answer was " + lastPress; } else { document.getElementById("ans").innerHTML = "We're sorry, but that is not correct. You said " + lastPress + ". The answer was " + answer[i]; } i++; console.log(i); document.getElementById("question").innerHTML = questions[i]; } document.getElementById("true").onclick = function(){itsTrue();getAns();}; //document.getElementById("true").onclick = function(){getAns()}; document.getElementById("false").onclick = function(){itsFalse(),getAns();}; // document.getElementById("false").onclick = function(){getAns()};
 <!DOCTYPE HTML> <html> <head> <title>Flash cards</title> </head> <body> <p id="question">This is where the question shows up</p> <p id="ans">This is where the result will show up</p> <button type="button" id="true">True</button> <button type="button" id="false">False</button> </body> </html>

在您的问题中,不清楚返回 undefined 的内容,所以我认为它是您的 HTML 中更新的内容。

您的代码返回 undefined 因为您每次点击都会增加i 但是你的数组只有两个元素长。 您需要处理所有问题都已显示的事实。 在这里,我只是将变量i重置为 0,但我不确定这是否适用于您的情况。

 var i = 0; var questions = ["1: Was the human migration caused by an ice age?", "2. this is a second question, true or false ? ", "3. did i got you with my last tricky question?"]; var answer = [true, true, false]; var lastPress; document.getElementById("question").innerHTML = questions[i]; function itsTrue() { lastPress = true; } function itsFalse() { lastPress = false; } function getAns() { if (answer[i] == lastPress) { document.getElementById("ans").innerHTML = "You're correct! The answer was " + lastPress; } else { document.getElementById("ans").innerHTML = "We're sorry, but that is not correct. You said " + lastPress + ". The answer was " + answer[i]; } i++; if(i >= questions.length) { i = 0; } document.getElementById("question").innerHTML = questions[i]; } document.getElementById("true").onclick = function(){itsTrue(); getAns() }; document.getElementById("false").onclick = function(){itsFalse(); getAns()};
 <!DOCTYPE HTML> <html> <head> <title>Flash cards</title> </head> <body> <p id="question">This is where the question shows up</p> <p id="ans">This is where the result will show up</p> <button type="button" id="true">True</button> <button type="button" id="false">False</button> </body> </html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM