javascript新手对web前端有疑问

javascript新手对web前端有疑问,javascript,frontend,Javascript,Frontend,编写一个名为“additionCalculator”的函数,该函数不接受任何参数,也不返回值。此函数将控制添加两个数字的基于web的计算器的逻辑(您可以通过这种方式制作更复杂的计算器,但我们现在只添加数字,以便我们能够关注这个概念)。网页上将有两个文本框,id分别为“input_one”和“input_two”,还有一个空div,id为“sum”。读取两个文本框的值,并在div中写入这两个值的加法。请注意,从页面中读取的每个值都是字符串,需要将它们转换为数字才能执行加法 到目前为止,我的代码是:

编写一个名为“additionCalculator”的函数,该函数不接受任何参数,也不返回值。此函数将控制添加两个数字的基于web的计算器的逻辑(您可以通过这种方式制作更复杂的计算器,但我们现在只添加数字,以便我们能够关注这个概念)。网页上将有两个文本框,id分别为“input_one”和“input_two”,还有一个空div,id为“sum”。读取两个文本框的值,并在div中写入这两个值的加法。请注意,从页面中读取的每个值都是字符串,需要将它们转换为数字才能执行加法

到目前为止,我的代码是:

    function additionCalculator(){
        var one=document.getElementById("input_one").value;
        var two=document.getElementById("input_two").value;
        var my_js_variable = one + two;
        document.getElementById("sum").innerHTML = my_js_variable;


    }

this is the output:

html: "<html>
<head>
<script src="myCode.js"></script>
</head>
<body>
<div id="sum">2-88</div>
<input type="text" id="input_one" value="2"/>
<input type="text" id="input_two" value="-88"/>
<button onclick="additionCalculator();">Add</button>
</body>
</html>"
expected: "<html>
<head>
<script src="myCode.js"></script>
</head>
<body>
<div id="sum">-86</div>
<input type="text" id="input_one" value="2"/>
<input type="text" id="input_two" value="-88"/>
<button onclick="additionCalculator();">Add</button>
</body>
</html>"
函数添加计算器(){
var one=document.getElementById(“input_one”).value;
var two=document.getElementById(“input_two”).value;
var my_js_variable=1+2;
document.getElementById(“sum”).innerHTML=my_js_变量;
}
这是输出:
html:“
2-88
添加
"
期望值:“
-86
添加
"
如何使其与前两个输入的值相加?谢谢

编辑:


由于下面的帮助,解决了答案谢谢

对HTML中的值使用parseInt,否则它将被视为字符串并连接为字符串

var my_js_variable=parseInt(一)+parseInt(二)

欢迎来到StackOverflow!那么,到目前为止的问题是什么呢?我的代码的输出是2-88,我希望它只给我-86(注意值是随机的,没有给定值教授已经输入了值)