从数组Javascript中

时间:2016-10-20 16:56:14

标签: javascript random numbers equation

window.onload = start;
var nr1 = Math.floor(Math.random()*50);
var nr2 = Math.floor(Math.random()*50);
var eq1 = nr1 +"+"+nr2;
var eq2 = nr1 + "-"+ nr2;
var eq3 = nr1 +"*"+ nr2;
var eq4 = nr1 +"/"+ nr2;

var arr = [eq1,eq2,eq3,eq4];
var eq = arr[Math.floor(Math.random()*arr.length),Math.floor(Math.random()*arr.length)];

我想练习一些东西,制作一个基本的数学游戏。我想用来生成随机数和随机方程式的代码,我希望从我的html中得出。我之所以选择运算符,是因为我希望整个方程式能够作为文本出现,我已经尝试过没有“”标记,我只是立即得到答案。

function start() {
document.getElementById("submit").onclick = submitans;
document.getElementById("question").innerHTML = eq;
}

这是我的启动函数,它生成等式。现在,如果你看这张照片,这在html中效果很好 - HTML。我的问题是,我不知道如何将方程连接到输入框中的“if(statement)”。因为脚本将命令作为文本读取,所以即使我写了

它也不起作用
   function submitans() {

    var correct = "Correct!";
    var wrong = "Wrong";      
    var inn = parseInt(document.getElementById("answer").value);

    for (var i = 0; i < eq.length; i++) {
       if(inn === eq[i])
        document.getElementById("input").innerHTML = "correct";
     }
  }

无论如何,我可以通过这个剧本达到我想要的目标吗?我坚持使用纯JavaScript,因为我只是一个初学者,我真的想学习它。所以我宁愿不使用jquery。

除此之外,我打算让我的脚本显示每个正确答案的分数,我想到目标最多有10个正确的答案,并且允许用户有5个错误。

5 个答案:

答案 0 :(得分:0)

我会改变你管理方程的方式......也许这样的东西可能有用:

function eq(x, y, op){
    // Find the result depending on the operation
    var res;
    switch (op){
        case: '+':
            res = x + y;
            break;
        case: '-':
            res = x - y;
            break;
        case: '*':
            res = x * y;
            break;
        case: '/':
            res = x / y;
            break;
        default:
            throw "Error";
    }
    // Or something like is more compact
    var res2 = op == '+' ? x+y : op == '-' ? x-y : op == '*' ? x*y : op == '/' ? x/y : null;

    return {
        text: x + op + y,
        res: res
    }
}

// ...
var eq1 = eq(nr1, nr2, '+');
var eq2 = eq(nr1, nr2, '-');
var eq3 = eq(nr1, nr2, '*');
var eq4 = eq(nr1, nr2, '/');

然后你只需要在HTML中显示eq.text并检查eq.res以与答案进行比较......也许你可以做一些更好的事情,但它应该有效

答案 1 :(得分:0)

你也可以使用eval函数来得到答案。 var answer = eval(document.getElementById("question").innerHTML)获得正确答案

答案 2 :(得分:0)

在第一段代码中,您可以为每个等式添加答案

var eq1 = nr1 +"+"+nr2;
var eq2 = nr1 + "-"+ nr2;
var eq3 = nr1 +"*"+ nr2;
var eq4 = nr1 +"/"+ nr2;
//answers
var ans1 = nr1 +"+"+nr2;
var ans2 = nr1 + "-"+ nr2;
var ans3 = nr1 +"*"+ nr2;
var ans4 = nr1 +"/"+ nr2;

然后当你得到随机方程式时,你也得到了它的答案

var arr = [eq1,eq2,eq3,eq4];
var ansArr = [ans1, ans2, ans3, ans4];
var eqNumber = Math.floor(Math.random()*arr.length);
var eq = arr[eqNumber];
var ans = ansArr[eqNumber];

并编辑最后一个代码段

function submitans() {
    var correct = "Correct!";
    var wrong = "Wrong";      
    var inn = parseInt(document.getElementById("answer").value);

    //for (var i = 0; i < eq.length; i++) {//delete
       if(inn == ans)
        document.getElementById("input").innerHTML = correct;
       else
        document.getElementById("input").innerHTML = wrong;
    //}//delete
 }

----------------------------------------------- ---------------------------------

或者代替所有这些,您也只能更改最后一段代码

function submitans() {
    var correct = "Correct!";
    var wrong = "Wrong";      
    var inn = parseInt(document.getElementById("answer").value);

    //for (var i = 0; i < eq.length; i++) {//delete
       if(inn == eval(eq))
        document.getElementById("input").innerHTML = correct;
       else
        document.getElementById("input").innerHTML = wrong;
    //}//delete
 }

eval会将文本作为javascript运行,它将返回等式的答案。

答案 3 :(得分:0)

eval()应该运作良好,试试这个:

function submitans() {
    var correct = "Correct!";
    var wrong = "Wrong";      
    var inn = parseInt(document.getElementById("answer").value);

    if(inn === eval(document.getElementById("question").innerHTML))
        document.getElementById("input").innerHTML = correct;
    else 
        document.getElementById("input").innerHTML = wrong;     
}

答案 4 :(得分:0)

there is difference between parseInt and Number

var inn = parseInt(document.getElementById("answer").value);
var inn = Number(document.getElementById("input").value);

========================

eval() you need to be careful with. more so in how javascript will interpret a given string of

var ans1 = eval( 4-3/2*39+3)

adding brackets will make sure you get correct result in how you want evel() to return an answer.

var ans2 = eval( (4-((3/2)*39))+3 );

var ans3 = eval(document.getElementById("equation").value);

will most likely return like ans1, be careful!

to get around issue of above...

var eq1a = nr1 +"+"+nr2;
var eq1b = nr1 + nr2
var eq2a = nr1 + "-"+ nr2; 
var eq2b = nr1 - nr2
var eq3a = nr1 +"*"+ nr2;
var eq3b = nr1 * nr2
var eq4a = nr1 +"/"+ nr2;
var eq4b = nr1 / nr2

easy enough above.....

taking next step

var eq1 = "nr1 + nr2"
var eq2 = "nr1 - nr2"
var eq3 = "nr1 * nr2"
var eq4 = "nr1 / nr2"

above is a string that you can do...

var nr1 = Math.floor(Math.random()*50);
var nr2 = Math.floor(Math.random()*50);
var eq1 = "nr1 + nr2"
document.getElementById("equation").innerHTML = eq1;
//and it will show correctly.
var ans = eval(eq1); 
// and it will give you the answer

and taking it one more step further....

var eq1 = "((nr1 + nr2) * 4 / 4)"
document.getElementById("equation").innerHTML = eq1.regex();
var ans = eval(eq1);

i forgot what needs to be placed in regex() brackets to remove the ( and ) brackets. so it does not print them out to equation html element.

====================

just be careful with eval() if application is critical application. your application could get hacked quickly.