我想通过文本输入从用户那里获取两个整数,找到两个数字之间的差值并将该数字除以 25。我该怎么做?
I want to get two integers from a user via a text input, find the difference between the two numbers and divide that number by 25. How do I do this?
我正在制作一个小网站作为测试。 JavaScript 和 HTML 形式非常新,所以我想我会把自己投入到我认为是最深的一端并试一试。
我正在尝试让一个整数显示在页面上,这是一些计算的结果。
我想找出第一个数字(当前值)和第二个数字(期望值)之间的差值,然后将该数字除以 25 并将其存储为变量。然后我想在消息中显示该变量。
我现在的 HTML :
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/stylesheet.css">
<title>MMR calculator</title>
</head>
<body>
<h1>Type in your current MMR, and your desired MMR and click "Calculate"</h1>
<form>
<input type="text" id="currentRating" placeholder="What is your current MMR?">
<input type="text" id="desiredRating" placeholder="What is your desired MMR?">
<input type="submit" onclick="calculate()">
</form>
<script src="js/main.js"></script>
</body>
</html>
我目前的JavaScript:
function calculate() {
var currentRating = document.getElementById("currentRating");
var desiredRating = document.getElementById("desiredRating");
var difference = desiredRating - currentRating;
var gamesToPlay = difference / 25;
document.write("You need to play " + gamesToPlay + " to get to " + desiredRating);
}
从另一个减去第一个字段,如果值不大于 0,则乘以 -1
。
除以 25。
希望此代码对您有所帮助:
html :
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/stylesheet.css">
<title>MMR calculator</title>
</head>
<body>
<h1>Type in your current MMR, and your desired MMR and click "Calculate"</h1>
<div>
<input type="text" id="currentRating" placeholder="What is your current MMR?">
<input type="text" id="desiredRating" placeholder="What is your desired MMR?">
<button onclick="calculate();">Calculate</button>
</div>
<script src="js/main.js"></script>
</body>
</html>
javascript :
function calculate() {
var currentRating = document.getElementById("currentRating").value;
var desiredRating = document.getElementById("desiredRating").value;
var gamesToPlay = (desiredRating - currentRating) / 25;
gamesToPlay = Math.abs( parseInt(gamesToPlay) );
alert("You need to play " + gamesToPlay + " to get to " + desiredRating);
}
你已经达到了 99%。你所要做的就是改变
var currentRating = document.getElementById("currentRating");
var desiredRating = document.getElementById("desiredRating");
进入
var currentRating = parseInt(document.getElementById("currentRating").value);
var desiredRating = parseInt(document.getElementById("desiredRating").value);
按照您的方式,这些变量只是包含 HTML(从技术上讲,DOM)元素本身,而不是其中的值。这将获取值,然后将它们转换为整数,以便您可以对它们进行数学运算。如果您这样做,您的网站就会完全按照您的意愿行事。
注意:
var currentRating = document.getElementById("currentRating").value;
是一个字符串(文本)值...要确定您可以做的 int 值
try{
var currentRatingInt = parseInt(currentRating);
}catch(e){
alert(currentRating + " is not an integer");
}
如果您想在页面中显示结果,您可以使用带有 ID 的 DIV 并执行以下操作:
document.getElementById("idOfYourDiv").innerHTML = "What you like to display in div";
我正在制作一个小网站作为测试。 JavaScript 和 HTML 形式非常新,所以我想我会把自己投入到我认为是最深的一端并试一试。
我正在尝试让一个整数显示在页面上,这是一些计算的结果。
我想找出第一个数字(当前值)和第二个数字(期望值)之间的差值,然后将该数字除以 25 并将其存储为变量。然后我想在消息中显示该变量。
我现在的 HTML :
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/stylesheet.css">
<title>MMR calculator</title>
</head>
<body>
<h1>Type in your current MMR, and your desired MMR and click "Calculate"</h1>
<form>
<input type="text" id="currentRating" placeholder="What is your current MMR?">
<input type="text" id="desiredRating" placeholder="What is your desired MMR?">
<input type="submit" onclick="calculate()">
</form>
<script src="js/main.js"></script>
</body>
</html>
我目前的JavaScript:
function calculate() {
var currentRating = document.getElementById("currentRating");
var desiredRating = document.getElementById("desiredRating");
var difference = desiredRating - currentRating;
var gamesToPlay = difference / 25;
document.write("You need to play " + gamesToPlay + " to get to " + desiredRating);
}
从另一个减去第一个字段,如果值不大于 0,则乘以 -1
。
除以 25。
希望此代码对您有所帮助:
html :
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/stylesheet.css">
<title>MMR calculator</title>
</head>
<body>
<h1>Type in your current MMR, and your desired MMR and click "Calculate"</h1>
<div>
<input type="text" id="currentRating" placeholder="What is your current MMR?">
<input type="text" id="desiredRating" placeholder="What is your desired MMR?">
<button onclick="calculate();">Calculate</button>
</div>
<script src="js/main.js"></script>
</body>
</html>
javascript :
function calculate() {
var currentRating = document.getElementById("currentRating").value;
var desiredRating = document.getElementById("desiredRating").value;
var gamesToPlay = (desiredRating - currentRating) / 25;
gamesToPlay = Math.abs( parseInt(gamesToPlay) );
alert("You need to play " + gamesToPlay + " to get to " + desiredRating);
}
你已经达到了 99%。你所要做的就是改变
var currentRating = document.getElementById("currentRating");
var desiredRating = document.getElementById("desiredRating");
进入
var currentRating = parseInt(document.getElementById("currentRating").value);
var desiredRating = parseInt(document.getElementById("desiredRating").value);
按照您的方式,这些变量只是包含 HTML(从技术上讲,DOM)元素本身,而不是其中的值。这将获取值,然后将它们转换为整数,以便您可以对它们进行数学运算。如果您这样做,您的网站就会完全按照您的意愿行事。
注意:
var currentRating = document.getElementById("currentRating").value;
是一个字符串(文本)值...要确定您可以做的 int 值
try{
var currentRatingInt = parseInt(currentRating);
}catch(e){
alert(currentRating + " is not an integer");
}
如果您想在页面中显示结果,您可以使用带有 ID 的 DIV 并执行以下操作:
document.getElementById("idOfYourDiv").innerHTML = "What you like to display in div";