Javascript 功能不是';不起作用。语法错误?

Javascript 功能不是';不起作用。语法错误?,javascript,function,Javascript,Function,我正在尝试创建一个脚本,它表示一个小学级乘法辅助工具。脚本选择两个随机数并将它们相乘。然后输入答案,如果答案正确,则表示“很好”,如果答案不正确,则表示“很好”,并返回提示(在正确回答之前,不会让您继续)。之后,它会询问您是否要继续“是”或“否”。如果是,请循环回初始函数,以返回另一组随机数进行乘法、清洗、冲洗和重复 由于某种原因,我的函数根本不起作用。您将在我尝试放置window.alerts的代码中看到,这只是为了让我知道它通过该特定函数运行。唉,这两种功能都没有警报。我知道这里的所有代码都

我正在尝试创建一个脚本,它表示一个小学级乘法辅助工具。脚本选择两个随机数并将它们相乘。然后输入答案,如果答案正确,则表示“很好”,如果答案不正确,则表示“很好”,并返回提示(在正确回答之前,不会让您继续)。之后,它会询问您是否要继续“是”或“否”。如果是,请循环回初始函数,以返回另一组随机数进行乘法、清洗、冲洗和重复

由于某种原因,我的函数根本不起作用。您将在我尝试放置window.alerts的代码中看到,这只是为了让我知道它通过该特定函数运行。唉,这两种功能都没有警报。我知道这里的所有代码都不是为了我要完成的,我不一定要找人来编写整个脚本(需要自己学习),但函数不会工作。语法?感谢您的帮助

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
    document.writeln ("<h2 style = 'color:blue' >");
    document.writeln ("Hello! Are you ready to learn some multiplication?");

    /* I don't believe either function is working properly and 
       I'm not entirely sure its syntax. I'm doin something wrong and 
       I'm gouging my eyes out!!! */
    do {    
        function start() {
            var button = document.getElementById("go");
            button.addEventListener("click", fetchnumbers, false);
            /* I add this to pop up during function 1 Just to check to see if its working*/
            window.alert ("function 1");
        }

        function fetchnumbers() {
            var math1 = Math.floor ((Math.random()*12)+1);
            var math2 = Math.floor ((Math.random()*12)+1);
            // I add this to pop up and display the numbers from the variables to ensure correct syntax for math function but heck no popup
            window.alert (math1 + math2 );
        }

        finish = window.prompt ("Are we done for today?\n\nYes = 1\nNo = 2");   
    } while (finish == 2);  
    </script>
</head>
<body>
    <br />
    <br />
    <form>
        <input id="go" type="button" value="Yeah!">
        <!--the id is referenced in the start function.-->
    </form>
</body>
</html>

document.writeln(“”);
document.writeln(“你好!你准备好学习乘法了吗?”);
/*我认为这两种功能都不能正常工作,而且
我不完全确定它的语法。我做错了什么事
我在挖我的眼睛*/
做{
函数start(){
var按钮=document.getElementById(“go”);
按钮。addEventListener(“单击”,获取数字,false);
/*我把它添加到函数1中的弹出窗口,只是为了检查它是否工作*/
window.alert(“功能1”);
}
函数fetchnumbers(){
var math1=Math.floor((Math.random()*12)+1);
var math2=Math.floor((Math.random()*12)+1);
//我将其添加到弹出窗口中,并显示变量中的数字,以确保数学函数的语法正确,但没有弹出窗口
window.alert(math1+math2);
}
finish=window.prompt(“今天的任务完成了吗?\n\nYes=1\nNo=2”);
}而(完成=2);



您的方法是。。。有点奇怪。。。但是主要的问题是,您的start函数从未被调用

您应该将函数移出do循环范围,正如我想象的那样,在加载页面时,您打算调用一次
start()
。目前,只能在do循环范围内调用函数

function start() {
    var button = document.getElementById("go");
    button.addEventListener("click", fetchnumbers, false);
    /* I add this to pop up during function 1 Just to check to see if its working*/
    window.alert ("function 1");
}

function fetchnumbers() {
    var math1 = Math.floor ((Math.random()*12)+1);
    var math2 = Math.floor ((Math.random()*12)+1);
    // I add this to pop up and display the numbers from the variables to ensure correct syntax for math function but heck no popup
    window.alert (math1 + math2 );
}
do { 
    finish = window.prompt ("Are we done for today?\n\nYes = 1\nNo = 2");   
} while (finish == 2);  
另外,我建议不要使用document.write函数。他们比他们的价值更麻烦


这里的示例

@Niloct:您在哪里调用
start
?此外,函数定义不应以块为单位。据我所知,我在“单击”表单按钮时调用了第一个函数。从第一个函数调用第二个函数。我不是100%确定你所指的块是不是{}?我看到了编辑,谢谢大家看一下。感觉像是这样一个noob帖子:(.设置断点,检查变量。调试101.我明白了..很抱歉我对javascript完全陌生。我写出来的方式一开始总是很奇怪,然后我就把它清理干净了。我总是把绒毛放进去只是为了检查代码。好的,是的,我添加了
window.addEventListener(“load”,start,false)
这样就调用了函数。我将把
do
语句拿出来,看看它的作用。谢谢你的帮助!@pimpedoutgees不必担心自己是新手。我们都是新手,javascript并不是最容易使用的语言。祝你课程顺利。