如何将提交按钮链接到随机数学方程?

时间:2022-07-22 21:23:57

I am trying to generate 3 random math equations. The math is basic arithmetic. I do not knwo how to link the Submit button to my function. Please help!

我试图生成3个随机数学方程式。数学是基本算术。我不知道如何将提交按钮链接到我的功能。请帮忙!

Code:

        var questionCorrect = 0;
        var questionAsked = 0;

        function basic()
        {
            function ask() 
                {
                    var a = Math.floor(Math.random() * 10) + 1;
                    var b = Math.floor(Math.random() * 10) + 1;
                    var op = ["*", "+", "-"][Math.floor(Math.random()*3)];
                    $("#basic").text("How much is " + a + " " + op + " " + b + "?") == eval( a + op + b);
                }     

                for(questionAsked=0; questionAsked<=2; questionAsked++)
                {
                    if(ask())
                    {
                        questionCorrect++;
                    }
                }

                //alert( "You got "+questionCorrect+"/"+questionAsked+" correctly");    

        }

HTML:

    <span id="basic"></span>

     <form id="basicAsk" name=form1>

               Answer:<input type="text" name="txtRadius" size=10>
               <input type="button" value="Submit Answer" onclick=""> 

    </form>

2 个解决方案

#1


1  

To link the function basic() to your button use:

要将函数basic()链接到按钮,请使用:

onclick="basic()"

But I don't understand what are you trying to do.

但我不明白你想做什么。

The if(ask()) never will be true because the function returns no value.

if(ask())永远不会成立,因为函数不返回任何值。

Can you explain what you want? Because without this we can´t help you.

你能解释一下你想要的吗?因为没有这个,我们无法帮助你。


Try changing your function ask to

尝试更改您的功能要求

function ask() {
            var a = Math.floor(Math.random() * 10) + 1;
            var b = Math.floor(Math.random() * 10) + 1;
            var op = [ "*", "+", "-" ][Math.floor(Math.random() * 3)];

            return eval(a + op + b) ==  prompt("How much is " + a + " " + op + " " + b + "?","");

        }

I think this is what you want.

我想这就是你想要的。

#2


0  

onclick="basic()"

Why are you wrapping the function within a function?

为什么要在函数中包装函数?

#1


1  

To link the function basic() to your button use:

要将函数basic()链接到按钮,请使用:

onclick="basic()"

But I don't understand what are you trying to do.

但我不明白你想做什么。

The if(ask()) never will be true because the function returns no value.

if(ask())永远不会成立,因为函数不返回任何值。

Can you explain what you want? Because without this we can´t help you.

你能解释一下你想要的吗?因为没有这个,我们无法帮助你。


Try changing your function ask to

尝试更改您的功能要求

function ask() {
            var a = Math.floor(Math.random() * 10) + 1;
            var b = Math.floor(Math.random() * 10) + 1;
            var op = [ "*", "+", "-" ][Math.floor(Math.random() * 3)];

            return eval(a + op + b) ==  prompt("How much is " + a + " " + op + " " + b + "?","");

        }

I think this is what you want.

我想这就是你想要的。

#2


0  

onclick="basic()"

Why are you wrapping the function within a function?

为什么要在函数中包装函数?