Javascript随机数循环未定义onclick

时间:2014-10-27 17:46:27

标签: javascript css loops random numbers

我在代码中找不到错误,每当我尝试运行它时,onclick返回错误“loopNums is not defined”。用户只能输入1到15之间的数字,循环将在用户输入和100之间生成10个随机数,以确定哪个是可分的。

<html>
<head>
<title></title>

<script type="text/javscript">

function loopNums() {
    var usernum = parseInt(document.getElementById("usernum").value);
    var msg = ""; 
    var sum = 0;

    if (usernum < 1 || usernum > 15) {
        msg = "Enter a number between 1 and 15";
    } else {
        for (var i = 0; i < 10; i++) {
            var rdnNumber = Math.floor(Math.random() * ( 1 + 100 - usernum )) + usernum;

            if (rdnNumber % usernum === 0)

                msg += "<div style='color: green'>Number generated: " + rdnNumber + ". It is divisible by " + usernum + "!</div>";
            else
                msg += "<div style='color: red'>Number generated: " + rdnNumber + ". It is not divisible by " + usernum + "!</div>";
        }
    }
    document.getElementById("results").innerHTML = msg;
}

</script>

<style type="text/css">

body {
    background-color: #996666;
    font-family: arial;
    color: #996666;
}
#contentwrap {
    background-color: #FFCC66;
    border: 8px #FF9933 solid;
    padding: 20px;
    width: 600px;
    margin: 20px auto 0px auto;
    border-radius: 25px;
    text-align: center;
}
#heading {
    font-size: 2.2em;
    border-bottom: 6px #663399 double;
    padding: 10px 0px 10px 0px;
}
#formdiv {
    text-align: center;
    padding-top: 25px;
}
.formtext {
    text-align: center;
    font-size: 1.29em;
    margin-top: 20px;
}
#usernum {
    border: 2px #663399 solid;
}
#result {
    font-size: 1em;
    color: #FF0000;
}

</style>

</head>
<body>

    <div id="contentwrap">
        <div id="heading">Loop Practice</div>
        <div id="formdiv">
            <form>Enter a number
                <br />
                <input type="text" id="usernum" />
                <p />
                <input type="button" value="Go For It!" onClick="loopNums()" />
            </form>
            <div id="results"></div>
        </div>
    </div>
    <!-- ends div#contentwrap -->
</body>
</html>

1 个答案:

答案 0 :(得分:0)

你的脚本的真正问题是&lt; script&gt;中的拼写错误。标签。它说javscript,应该是javascript。