【发布时间】:2014-05-15 18:57:22
【问题描述】:
用户只能输入一个介于 1 到 15 之间的数字,所以如果输入超出 范围,输出错误并从函数中“返回”。如果输入好, 然后继续。
创建一个循环,生成 10 个介于 1 和 100 之间的随机数。
我知道我的 javascript 不正确,我卡在 atm 上。
<html>
<head>
<title></title>
<script type="text/javascript">
function loopNums() {
var usernum = parseInt( document.getElementById("usernum").value );
var msg = ""; // "" is the NULL string
var ;
for( var i = 1; i <= 15; i++ ) {
if( i % usernum == 0) {
msg = msg + "<div>" i + "is NOT divisible by " usernum + "</div>";
msg = msg + "<div>You entered " + i + "</div>";
sum = sum + i;
} // ends the for loop
msg = msg + "<div>The sum is " + sum + "</div>";
document.getElementById("results").innerHTML = msg;
} // end loopNums() function
</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>
【问题讨论】:
-
将代码粘贴到您的问题中。小提琴链接用于演示,而不是代码存档。
-
打开你的控制台,你会发现一个错误信息...
标签: javascript loops if-statement random numbers