错了还是对吗?使用Javascript

时间:2016-01-14 07:43:14

标签: javascript html

我有2个按钮,正确&错误。当屏幕上弹出随机问题(vragen)时,其索引号[1]需要与答案(antw)索引号[1]相同。如果按下G按钮它的好,它必须说yay,坏noo和点击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!");
    }
}
};

和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" />

2 个答案:

答案 0 :(得分:1)

试试这个。编辑:这个适用于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!");
        }
    });
});

答案 1 :(得分:1)

正如您自己解释的那样,您要做的是检查答案的“真实性”(G = true和F = false)是否等于antw[randans]的值。

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

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