使用函数显示随机数,然后 ifelse 语句分配星期几

Using function to display random numbers, then ifelse statement to allocate days of the week

当用户点击按钮时,脚本会生成一个随机数,然后显示相应的星期几。下面的代码是我到目前为止所拥有的,唉,它似乎不起作用。 我已经调试过了,显然我的函数没有定义?!

 <!DOCTYPE html>
    <html>

    <body>

    <p>Click the button to display a random number between 1 and 7</p>

    <button onclick="myFunction()">Get Random</button>   

    <p id="task1"></p>

    <script>
        function myFunction() {
        var x = Math.floor((Math.random() * 7 + 1);
        document.getElementById("task1").innerHTML = x;
        }

        if (x===1)
        {
            document.write("today is sunday");
        }
        else if(x===2)
        {
            document.write("today is monday");
        }
        else if(x===3)
        {
            document.write("today is tuesday");
        }
        else if(x===4)
        {
            document.write("today is wednesday");
        }
        else if(x===5)
        {
            document.write("today is thursday");
        }
        else if(x===6)
        {
            document.write("today is friday");
        }
        else if(x===7)
        {
            document.write("today is saturday");
        }

    </script>

    </body>

    </html>

您的 var xlocalmyFunction 主体中定义的变量。它无法在您的功能之外访问(在您的所有 if 中)。

您的 var x = Math.floor(Math.random() * 7 + 1); 语句中还有多余的左括号。

更正版本:

function myFunction() {
    var x = Math.floor(Math.random() * 7 + 1);
    document.getElementById("task1").innerHTML = x;        

    if (x===1)
    {
        document.write("today is sunday");
    }
    else if(x===2)
    {
        document.write("today is monday");
    }
    else if(x===3)
    {
        document.write("today is tuesday");
    }
    else if(x===4)
    {
        document.write("today is wednesday");
    }
    else if(x===5)
    {
        document.write("today is thursday");
    }
    else if(x===6)
    {
        document.write("today is friday");
    }
    else if(x===7)
    {
        document.write("today is saturday");
    }
}

你有一个额外的括号,代码在函数呈现之前抛出异常。

var x = Math.floor(Math.random() * 7 + 1); document.getElementById("task1").innerHTML = x;

您可以使用浏览器控制台查看到底出了什么问题。

您在 Math.floor 函数中多了一个括号。尝试 运行 以下内容,您会发现它有效。

function myFunction() {
    var x = Math.floor(Math.random() * 7 + 1);
    document.getElementById("task1").innerHTML = x;
  
    if (x===1)
    {
        document.write("today is sunday");
    }
    else if(x===2)
    {
        document.write("today is monday");
    }
    else if(x===3)
    {
        document.write("today is tuesday");
    }
    else if(x===4)
    {
        document.write("today is wednesday");
    }
    else if(x===5)
    {
        document.write("today is thursday");
    }
    else if(x===6)
    {
        document.write("today is friday");
    }
    else if(x===7)
    {
        document.write("today is saturday");
    }
}
    <p>Click the button to display a random number between 1 and 7</p>

    <button onclick="myFunction()">Get Random</button>   

    <p id="task1"></p>

It can be much easier and shorter if you use and object days for example

//declare the object that holds the days.
    var days=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];

//declare your function that's triggered when clicking the button.
    function myFunction(){

//declare your variable x that generates random numbers between 0 and 7 ,
// it starts from 0 because the first day "sunday" is days[0] not days[1].
    var x = Math.floor(Math.random()*7);

//write the result in the p element
    document.getElementById("task1").innerHTML ="today is "+ days[x];
    }
<button onclick="myFunction()">Click Here</button>
<p id="task1"></p>