Ich probiere ein einfaches Programm aus, das auf Konsole ausgibt. Alles funktioniert gut, nur unerwartet undefined und ich weiß nicht, warum ich es bekomme. Jeder bitte erklären was falsch ist und wie kann ich meinen Code besser machen. DankIch bekomme undefined in JavaScript und kann es nicht herausfinden warum
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()) funktioniert nicht. Probieren Sie einfach options() und entfernen Sie die Rückgabe false in der Optionsfunktion –
dank @Jonas w es jetzt funktioniert. – knight
können Sie mir bitte helfen, wie kann ich readio Tasten mit Optionen Funktionswerte erstellen? – knight