# JS中的计算器

[英]Calculator in JS

I was trying to make a really simple & basic calculator with JS. 我试图用JS制作一个非常简单的基本计算器。 For the "addition" portion of the calculator the numbers that are put in won't add properly... FOR EXAMPLE: 12 + 12 comes out as "1212" not 24 OR 6 + 15 comes out as "615" not 21 Can anyone tell me why? 对于计算器的“加法”部分，放入的数字将无法正确添加...例如：12 + 12表示为“1212”而不是24或6 + 15表示为“615”而不是21表示有谁告诉我为什么？

``````<!DOCTYPE html>
<html>
<button onclick="Divide()">Divide</button> </br>
<button onclick="Multiply()">Multiply</button> </br>
<button onclick="Subtract()">Subtract</button> </br>
<script>
function Divide (){
var dividend = prompt("What is the dividend?")
var divisor = prompt("What is the divisor")
var answer = prompt(dividend / divisor)
}
function Multiply (){
var Number1 = prompt("What is the first number")
var Number2 = prompt("What is the second number")
var answer = prompt(Number1 * Number2)
}
function Subtract (){
var Number1 = prompt("What is the first number")
var Number2 = prompt("What is the second number")
var answer = prompt(Number1 - Number2)
}
var Number1 = prompt("What is the first number")
var Number2 = prompt("What is the second number")
var answer = prompt(Number1 + Number2)
}

</script>

<body>

</body>
</html>
``````

Javascript is taking user input as string data. Javascript将用户输入作为字符串数据。 The `+` symbol concatenates strings. `+`符号连接字符串。 For addition, you want to convert the string data to integers with parseInt(): 另外，您希望使用parseInt（）将字符串数据转换为整数：

`var answer = prompt(parseInt(Number1) + parseInt(Number2));`