Javascript ReferenceError:HtmlButtoneElement.button.onclick上未定义答案
我是编程界的新手,一直在从事一个琐碎的游戏风格的项目。我遇到的问题如下:“uncaughtreferenceerror:answer未在HTMLButtonElement.button.onclick中定义”。 我的问题如下:如何在按下答案时不存储我的问题答案?在我的代码中定义答案的更好方法是什么?任何帮助都将不胜感激 HTMLJavascript ReferenceError:HtmlButtoneElement.button.onclick上未定义答案,javascript,html,referenceerror,Javascript,Html,Referenceerror,我是编程界的新手,一直在从事一个琐碎的游戏风格的项目。我遇到的问题如下:“uncaughtreferenceerror:answer未在HTMLButtonElement.button.onclick中定义”。 我的问题如下:如何在按下答案时不存储我的问题答案?在我的代码中定义答案的更好方法是什么?任何帮助都将不胜感激 HTML 益智问答游戏 高尔夫球手的琐事 `` JAVASCRIPT // Keeping score var unanswered = 0; var questi
益智问答游戏
高尔夫球手的琐事
``
JAVASCRIPT
// Keeping score
var unanswered = 0;
var questionIndex = 0;
var score = 0;
var questions = 0;
var answer;
function Quiz(questions) {
this.score = 0;
this.questions = questions;
this.questionIndex = 0;
}
function getQuestionIndex() {
return this.questions[this.questionIndex];
}
function endGame() {
return this.questions.length === this.questionIndex;
}
function guess(answer) {
if (this.getQuestionIndex() === correctAnswer(answer)) {
this.score++;
}
this.questionIndex++;
}
// functions for questions
function Question(text, choices, answer) {
this.text = text;
this.choices = choices;
this.answer = answer;
}
// check user answer
function correctAnswer(choice) {
return choice === this.answer;
}
// have questions appear if game is still going
function populate() {
console.log("populating");
if (endGame()) {
showScores();
}
else {
var element = document.getElementById("question");
element.innerHTML = getQuestionIndex().text;
// have options appear for each question
var choices = getQuestionIndex().choices;
for (var i = 0; i < choices.length; i++) {
var element = document.getElementById("option" + i);
element.innerHTML = choices[i];
guess("btn" + i, choices[i]);
}
showProgress()
}
}
// store user guess
function guess(id) {
var button = document.getElementById(id);
button.onclick = function () {
questionIndex++;
populate();
guess(answer);
}
}
// show which question player is on
function showProgress() {
var currentQuestionNumber = questionIndex + 1;
var element = document.getElementById("progress");
element.innerHTML = "Question " + currentQuestionNumber + " of " + questions.length;
}
// display scores at end of game
function showScores() {
var gameOver = "<h1>Results</h1>" + "<h2 class='corr score'> Correct Answers: " + score + "<h2>" + "<br>" + "<h2 class = 'wrong score'>Wrong Answers: " + (questions.length - score) + "<h2 class = 'unanswered score'>Unanswered: " + "<h2>";
var results = document.getElementById("trivia");
results.innerHTML = gameOver;
}
// sets of questions, options, answers
var questions = [
new Question("Where was the game of golf originally founded?",
["Scotland", "China", "England", "United States"],
"Scotland"),
new Question("Who is the only female golfer to make a cut at a PGA Tour event?",
["Michelle Wie", "Annika Sorensteim", "Lexi Thompson", "Babe Zaharias"],
"Babe Zaharias"),
new Question("What is the name for a hole-in-one on a par five?",
["Triple Eagle", "Double Ace", "Condor", "Albatross"],
"Condor"),
new Question("Who holds the record for the most PGA Tour victories?",
["Tiger Woods", "Jack Nicklaus", "Ben Hogan", "Sam Snead"],
"Sam Snead"),
new Question("What percentage of golfers will never achieve a handicap of 18 or less?",
["50 percent", "73 percent", "80 percent", "91 percent"],
"80 percent"),
new Question("How many dimples are on a standard regulation golf ball?",
["336", "402", "196", "468"],
"336"),
new Question("Who was considered the first professional golfer in history?",
["Bobby Jones", "Byron Nelson", "Walter Hagen", "Old Tom Morris"],
"Walter Hagen"),
new Question("Who is the youngest player to win the Masters?",
["Tiger Woods", "Jack Nicklaus", "Jordan Speith", "Arnold Palmer"],
"Tiger Woods")
];
populate();
var intervalId;
$("#btn").on("click", run);
// The run function sets an interval
function run() {
clearInterval(intervalId);
}
var timeLeft = 10;
var displayClock = document.getElementById('timer');
var timerId = setInterval(countdown, 1000);
function countdown() {
if (timeLeft === 0) {
unanswered++;
questionIndex++;
populate();
alert("You did not answer in time!");
timeLeft = 10;
// reset timer, pull question
run();
} else {
displayClock.innerHTML = timeLeft + ' seconds remaining';
timeLeft--;
}
}
run();
//记分
未回答的var=0;
var指数=0;
var得分=0;
var=0;
var回答;
功能测验(问题){
这个分数=0;
这个。问题=问题;
该指数=0;
}
函数getQuestionIndex(){
返回此.questions[this.questionIndex];
}
函数endGame(){
返回this.questions.length==this.questionIndex;
}
函数猜测(答案){
if(this.getQuestionIndex()==correctAnswer(答案)){
这个是.score++;
}
这个.questionIndex++;
}
//问题的功能
功能问题(文本、选项、答案){
this.text=文本;
这个。选择=选择;
这个答案=答案;
}
//检查用户答案
功能正确答案(选择){
返回选项===this.answer;
}
//如果游戏还在进行,有问题出现吗
函数填充(){
控制台日志(“填充”);
如果(endGame()){
showScores();
}
否则{
var元素=document.getElementById(“问题”);
element.innerHTML=getQuestionIndex().text;
//为每个问题显示选项
var choices=getQuestionIndex();
for(var i=0;i“+”错误答案:“+(questions.length-score)+”未回答:“+”;
var results=document.getElementById(“琐事”);
results.innerHTML=gameOver;
}
//一组问题、选项、答案
变量问题=[
新问题(“高尔夫运动最初是在哪里创立的?”,
[“苏格兰”、“中国”、“英格兰”、“美国”],
“苏格兰”),
新问题(“谁是唯一在PGA巡回赛中得分的女性高尔夫球手?”,
[“Michelle Wie”、“Annika Sorensteim”、“Lexi Thompson”、“Babe Zaharias”],
“Babe Zaharias”),
一个新的问题:“一个洞的名字在一个五杆上?”
[“三鹰”、“双王牌”、“秃鹰”、“信天翁”],
“秃鹰”),
新问题(“谁保持了PGA巡回赛最多胜利的记录?”,
[“老虎伍兹”、“杰克·尼克劳斯”、“本·霍根”、“山姆·斯奈德”],
“Sam Snead”),
新问题(“什么百分比的高尔夫球手永远不会达到18或更少的障碍?”,
[“百分之五十”、“百分之七十三”、“百分之八十”、“百分之九十一”],
“百分之八十”),
新问题(“标准规则高尔夫球上有多少酒窝?”,
["336", "402", "196", "468"],
"336"),
新问题(“谁被认为是历史上第一位职业高尔夫球手?”,
[“鲍比·琼斯”、“拜伦·纳尔逊”、“沃尔特·哈根”、“老汤姆·莫里斯”],
“沃尔特·哈根”),
新问题(“谁是赢得大师赛的最年轻球员?”,
[“老虎伍兹”、“杰克·尼克劳斯”、“乔丹·斯佩斯”、“阿诺德·帕尔默”],
“老虎伍兹”)
];
填充();
var有效期;
美元(“#btn”)。在(“单击”,运行);
//run函数设置一个间隔
函数运行(){
clearInterval(intervalId);
}
var timeLeft=10;
var displayClock=document.getElementById('timer');
var timerId=设置间隔(倒计时,1000);
函数倒计时(){
if(timeLeft==0){
未回答的++;
问题索引++;
填充();
警惕(“你没有及时回答!”);
时间间隔=10;
//重置计时器,拉问题
run();
}否则{
displayClock.innerHTML=timeLeft+“剩余秒数”;
时间限制--;
}
}
run();
我想你在这里面临另一个问题。以下是从脚本中删除的两个函数:
guess(任意)
version 1
function guess(answer) {
if (this.getQuestionIndex() === correctAnswer(answer)) {
this.score++;
}
this.questionIndex++;
}
guess(任意)
version 2
function guess(id) {
var button = document.getElementById(id);
button.onclick = function () {
questionIndex++;
populate();
guess(answer);
}
}
有两个函数名为guess()
。尽管两个值的名称各不相同,但从Javascript的角度来看,它们都是这样的:
function guess(value){}
JS怎么知道你打算给他们中的哪一个打电话
至少重命名其中一个,以使函数名完全明确无误。然后再试一次。您可以发布html吗?我认为您的猜测函数需要另一个参数<代码>函数猜测(id,答案){…}从您使用的“this”判断,您的测验对象似乎没有包含您希望它包含的所有内容。发布html后,我们可以测试它并帮助您修复。您不应该使用全局“问题”和“this.questions”等来向
function guess(value){}