我需要帮助来尝试获取随机生成的数字以将其带入 IF语句。 这是我最大的能力来解释我的问题,也有 简化了注释,以帮助像我这样的新手更轻松地了解 继续。底部的纯文本副本如果太乱了,因为这是我的拳头 问题。
<body>
//This button once clicked will call the function ranNum()
<input type="button" onClick="ranNum()" value="Generate Random Number
Between 1 and 20" />
//Where the randomly generated number is displayed ranNumber
<p id="ranNumber"></p>
/*Where the texted based on the Random Number generated is
displayed*/
<p id="numText"></p>
<script>
//This is the start of the function
function ranNum() {
/*Here is where the Random Number is place in
the variable ranNumber and then written to the p tag with
the same id This happens once generated from the code
below*/
document.getElementById("ranNumber").innerHTML =
/*Here is where I get stuck, Ive tried to give the Random
Number a variable name GeneratedNumber but it doesnt seem
to carry through to the IF statement. If anyone can help
explain a simple fix which Im sure it is and a little
explanation. Id be very great full*/
var GeneratedNumber = Math.floor(Math.random() * 20) + 1;
/*Here I set a variable conclusion to be carried through the
IF statement.*/
var conclusion
/*Here I carried the variable GeneratedNumber which I thought
should hold the Random Number generated into the IF statement.
It was to display the text within the IF statement if the
number was equal to or below 10*/
if (GeneratedNumber = < 10) {
conclusion = "The number generated 'numbervar' is less than
or equal to 10"
/*Here any number that didnt meet the above criteria was to
display the text below within the else*/
} else {
conclusion = "The number generated “numbervar” is greater
than 10"
}
/*Here the id numText is called to write the result of the IF
statement or the conclusion to the page*/
document.getElementById("numText").innerHTML = conclusion;
}
</script>
</body>
//Plain copy of text with no comments
<body>
<input type="button" onClick="ranNum()" value="Generate Random Number
Between 1 and 20" />
<p id="ranNumber"></p>
<p id="numText"></p>
<script>
function ranNum() {
document.getElementById("ranNumber").innerHTML =
var GeneratedNumber = Math.floor(Math.random() * 20) + 1;
var conclusion
if (GeneratedNumber = < 10) {
conclusion = "The number generated 'numbervar' is less than
or equal to 10"
} else {
conclusion = "The number generated “numbervar” is greater
than 10"
}
document.getElementById("numText").innerHTML = conclusion;
}
</script>
</body>
答案 0 :(得分:0)
您的代码有几个问题,请检查语法! ;)
这只是使代码正常工作的最小改进
```javascript
<script>
function ranNum() {
document.getElementById("ranNumber").innerHTML =
GeneratedNumber = Math.floor(Math.random() * 20) + 1;
var conclusion
if (GeneratedNumber <= 10) {
conclusion = "The number generated 'numbervar' is less than or equal to 10"
} else {
conclusion = "The number generated “numbervar” is greater than 10"
}
document.getElementById("numText").innerHTML = conclusion;
}
</script>
<input type="button" onClick="ranNum()" value="Generate Random Number
Between 1 and 20" />
<p id="ranNumber"></p>
<p id="numText"></p>
```
答案 1 :(得分:0)
代码中只有一些小错误。这应该起作用:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Fix</title>
</head>
<body>
<input type="button" onClick="ranNum()" value="Generate Random Number
Between 1 and 20" />
<p id="ranNumber"></p>
<p id="numText"></p>
<script>
function ranNum() {
//document.getElementById("ranNumber").innerHTML =
var GeneratedNumber = Math.floor(Math.random() * 20) + 1;
var conclusion
if (GeneratedNumber <= 10) {
conclusion = "The number generated 'numbervar' is less than or equal to 10";
} else {
conclusion = "The number generated “numbervar” is greater than 10";
}
document.getElementById("numText").innerText = conclusion;
}
</script>
</body>
</body>
</html>
答案 2 :(得分:0)
您是在随机数计算中定义变量的,所以我将它们分开了。
function ranNum() {
var GeneratedNumber = Math.floor(Math.random() * 20) + 1;
var ranNumber = document.getElementById("ranNumber");
ranNumber.innerHTML = GeneratedNumber;
var conclusion
if (GeneratedNumber <= 10) {
conclusion = "The number generated 'numbervar' is less than or equal to 10 "
} else {
conclusion = "The number generated “numbervar” is greater than 10 "
}
document.getElementById("numText").innerHTML = conclusion;
}
<input type="button" onClick="ranNum()" value="Generate Random Number
Between 1 and 20" />
<p id="ranNumber"></p>
<p id="numText"></p>
答案 3 :(得分:0)
这是我对StackOverflow问题的第一个答复,很抱歉,如果某些格式很奇怪,但是原始格式中的范围存在一些问题
document.getElementById("ranNumber").innerHTML = GeneratedNumber =
Math.floor(Math.random() * 20) + 1;
除了某些比较运算符的使用不正确之外,例如(a = <10)不能代替(a <= 10)使用。最后,我还更改了结论字符串,以插入自像个虫子一样!祝您好运
function ranNum() {
var GeneratedNumber = Math.floor(Math.random() * 20) + 1;
var conclusion
if (GeneratedNumber <= 10) {
conclusion = `The number generated ${GeneratedNumber} is less than or equal to 10`;
} else {
conclusion = `The number generated ${GeneratedNumber} is greater than 10`;
}
document.getElementById("numText").innerHTML = conclusion;
}