如何在html中添加两个给定数字

时间:2018-03-14 11:30:43

标签: javascript html

所以我的代码只是把它们加在一起如果我把4 + 4放到它给我44但我想要像4 + 4 = 8

<html>
<head>
<title>Laskeminen</title>
<body>

<script language="JavaScript">
first = prompt("Enter your first number.");
last = prompt("Enter your second number.");
</script>

<p id="first,last"></p>

<script>
var y = first
var z = last
var x = y + z;
document.getElementById("first,last").innerHTML = x;
</script> 

</head>
</body>
</html>

6 个答案:

答案 0 :(得分:1)

您可以使用parseInt()

<html>
<head>
<title>Laskeminen</title>
<body>

<script language="JavaScript">
first = prompt("Enter your first number.");
last = prompt("Enter your second number.");
</script>

<p id="first,last"></p>

<script>
var y = parseInt(first)
var z = parseInt(last)
var x = y + z;
document.getElementById("first,last").innerHTML = x;
</script> 

</head>
</body>
</html>

答案 1 :(得分:1)

使用Number()

<html>
<head>
<title>Laskeminen</title>
<body>

<script language="JavaScript">
first = prompt("Enter your first number.");
last = prompt("Enter your second number.");
</script>

<p id="first,last"></p>

<script>
var y = Number(first);
var z = Number(last);
var x = y + z;
document.getElementById("first,last").innerHTML = x;
</script> 

</head>
</body>
</html>

答案 2 :(得分:0)

prompt返回执行字符串连接的字符串。使用parseInt执行添加。你也有一些不必要的代码:

var first = prompt("Enter your first number.");
var last = prompt("Enter your second number.");
var x = parseInt(first) + parseInt(last);
document.getElementById("first,last").innerHTML = x;
<p id="first,last"></p>

答案 3 :(得分:0)

在第二个添加脚本中,您需要解析输入,因为它当前是一个字符串。 见下文

first = prompt("Enter your first number.");
console.log('User input is: ', typeof first)

所以因为这些是你添加文本时的字符串,例如。

var text = 'ab';
var text1 = 'c';
if(text+text1 == 'abc'){
console.log('These have been added together and are equal')
}

因此,如果您使用的是整数或整数,您需要解析parseInt(**variabletoParse**)parseFloat(**variabletoParse**);

所以你需要做的就是打击:

<html>
<head>
<title>Laskeminen</title>
<body>

<script language="JavaScript">
first = prompt("Enter your first number.");
last = prompt("Enter your second number.");
</script>

<p id="first,last"></p>

<script>
var y = parseFloat(first);
var z = parseFloat(last);
var x = y + z;
document.getElementById("first,last").innerHTML = x;
</script> 

</head>
</body>
</html>

答案 4 :(得分:0)

您可以对值使用parseFloat()将其转换为可执行数学运算的数学浮点值:

var first = prompt("Enter your first number.");
var last = prompt("Enter your second number.");

var y = parseFloat(first);
var z = parseFloat(last);
var x = y + z;
document.getElementById("first,last").innerHTML = x;
<p id="first,last"></p>

但如果您只想integers,那么您可以parseInt()

var first = prompt("Enter your first number.");
var last = prompt("Enter your second number.");

var y = parseInt(first);
var z = parseInt(last);
var x = y + z;
document.getElementById("first,last").innerHTML = x;
<p id="first,last"></p>

答案 5 :(得分:0)

   <html>
<head>
<title>Laskeminen</title>
<body>
<p id="first,last"></p>
<script language="JavaScript">
first = prompt("Enter your first number.");
last = prompt("Enter your second number."); 
var y = parseInt(first);
var z = parseInt(last);
var x = y + z;
document.getElementById("first,last").innerHTML = x;
</script> 

</head>
</body>

</body>
</html>