【发布时间】:2016-10-28 13:54:39
【问题描述】:
我正在尝试一个输出到控制台的简单程序。一切正常,只是变得意外未定义,我不知道为什么会得到它。任何人都请解释什么是错的,我怎样才能让我的代码更好。谢谢
var next = document.getElementById('next');
var questions =
[
{
question: "Color of sky is ?",
choices: ["Green","White", "Blue", "Red"],
answer: 2
},
{
question: "Color of milk is ?",
choices: ["Green","White", "Blue", "Red"],
answer: 1
},
{
question: "Color of Grass is ?",
choices: ["Green","White", "Blue", "Red"],
answer: 0
}
];
var counter = 0;
function loadQuestion () {
var obj = questions[counter];
var quest = obj.question;
var choice = obj.choices;
function options() {
choice.forEach(function(val){
console.log(val);
return false;
});
}
var answer = obj.answer;
counter++;
console.log ( " Question : " + quest );
console.log( options() );
console.log( answer );
}
next.onclick = function() {
if ( counter < questions.length ) {
loadQuestion();
}
};
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="quiz-container">
<button id="next"> Next Question </button>
</div>
</body>
</html>
【问题讨论】:
-
console.log( options() ) 不起作用。尝试仅 options() 并删除选项函数中的 return false
-
感谢@Jonas 现在可以使用了。
-
你能指导我如何创建带有选项功能值的读取按钮吗?
-
打开另一个问题 -> 得到另一个答案
标签: javascript arrays loops object foreach