错还是对?Javascript

错还是对?Javascript,javascript,html,Javascript,Html,我有两个按钮,对和错。当屏幕上弹出随机问题(vragen)时,其索引编号[1]需要与答案(antw)索引编号[1]相同。如果按下G键,它会说“是啊,坏了,不”,当点击F键时,它会说“是啊,坏了,不”,同样的,但反过来说 window.onload = function start(){ var questions = [ "1 x 1 = 2", "1 x 1 = 1", "2 x 2 = 4", "7 x 7 = 49", "1 x 8 = 9",

我有两个按钮,对和错。当屏幕上弹出随机问题(vragen)时,其索引编号[1]需要与答案(antw)索引编号[1]相同。如果按下G键,它会说“是啊,坏了,不”,当点击F键时,它会说“是啊,坏了,不”,同样的,但反过来说

window.onload = function start(){

var questions = [  "1 x 1 = 2",
    "1 x 1 = 1",
    "2 x 2 = 4",
    "7 x 7 = 49",
    "1 x 8 = 9",
    "8 x 1 = 8",
    "5 x 5 = 25",
    "3 x 5 = 2"
];
var answers = [    false,
                true,
                true,
                true,
                false,
                true,
                true,
                false
];

var rand = questions[Math.floor(Math.random() * questions.length)];
var randans = answers[Math.floor(Math.random() * answers.length)];
document.getElementById('quizzy').innerHTML = rand + "<br>";

document.getElementById('G').onclick = check1;
document.getElementById('F').onclick = check2;
function check1(){
    if(rand == randans){
        alert("a!");
    }
}
};
window.onload=函数开始(){
变量问题=[“1 x 1=2”,
“1 x 1=1”,
“2 x 2=4”,
“7 x 7=49”,
“1 x 8=9”,
“8x1=8”,
“5 x 5=25”,
“3 x 5=2”
];
var answers=[false,
是的,
是的,
是的,
假,,
是的,
是的,
假的
];
var rand=questions[Math.floor(Math.random()*questions.length)];
var randans=answers[Math.floor(Math.random()*answers.length)];
document.getElementById('quizzy')。innerHTML=rand+“
”; document.getElementById('G')。onclick=check1; document.getElementById('F')。onclick=check2; 函数检查1(){ if(rand==randans){ 警惕(“a!”); } } };
还有HTML

<div id="section">
<h2>Goed of Fout?</h2>
<div id="quizzy"><script src="quiz.js"></script></div>
<img src="images/G_03.gif" width="200px" class="GF" id="G"/>
<img src="images/F_03.gif" width="200px" class="GF" id="F"/>
<img src="images/chick_03.png" width="100px" id="chick" />

歌德·福特?

试试这个。编辑:这个可以与jQuery一起使用

$( document ).ready(function() {

    var vragen = ["1 x 1 = 2",
        "1 x 1 = 1",
        "2 x 2 = 4",
        "7 x 7 = 49",
        "1 x 8 = 9",
        "8 x 1 = 8",
        "5 x 5 = 25",
        "3 x 5 = 2"
    ];
    var antw = [false,
        true,
        true,
        true,
        false,
        true,
        true,
        false
    ];

    var a = Math.floor(Math.random() * vragen.length),
        rand = vragen[a];
    $('#quizzy').innerHTML = rand + "<br>";

    $(".GF").onclick(function(){
        if (antw[a] === true) {
            alert("a!");
        } else {
            alert("boo!");
        }
    });
});
$(文档).ready(函数(){
var vragen=[“1 x 1=2”,
“1 x 1=1”,
“2 x 2=4”,
“7 x 7=49”,
“1 x 8=9”,
“8x1=8”,
“5 x 5=25”,
“3 x 5=2”
];
var antw=[false,
是的,
是的,
是的,
假,,
是的,
是的,
假的
];
var a=Math.floor(Math.random()*vragen.length),
兰德=弗拉根[a];
$('#quizzy')。innerHTML=rand+“
”; $(“.GF”).onclick(函数(){ if(antw[a]==真){ 警惕(“a!”); }否则{ 警惕(“嘘!”); } }); });
正如您自己所解释的,您要做的是检查答案的“真实性”(G=true和F=false)是否等于
antw[randans]

所以在
check1
中是
if(antw[randans]==true){


您根本不需要
rand
,因为您想将答案链接到相应的问题,而不是随机的问题…

共享HTML/fiddle Mate您不需要检查rand==randans,因为randans与此无关rand@DennisVanZanten,请不要使用荷兰语变量名。它应该有相同的索引,但最好不要使用o将结构更改为[{'question':“1x1=2”,“answer”:false},{'question':“1x1=1”,“answer”:true}],这样就不需要两个索引了。