I'm having trouble understanding the problem with my code. The goal is to calculate the sum of every number in a given range (for example, entering 1 and 2 should output 3). However, when I input 1 and 3, the output unexpectedly becomes 12.
function Sum(){
var int1=document.getElementById("TextBox1").value;
var int2=document.getElementById("TextBox2").value;
for(i=int1; i<=int2; i++){
var total=int1;
total+=i;
if(i==int2){
alert("The sum of all numbers is "+total);
}
}
}
function Sum(){
var int1=document.getElementById("TextBox1").value;
var int2=document.getElementById("TextBox2").value;
for(i=int1; i<=int2; i++){
var total=int1;
total+=i;
if(i==int2){
alert("The sum of all numbers is "+total);
}
}
}
<html>
<head>
<title>
For Sum Exercise
</title>
</head>
<body>
<script>
</script>
User Number 1:<input id="TextBox1"><br><br>
User Number 2:<input id="TextBox2"><br><br>
<button onClick="Sum()">Sum</button><br>
</body>
</html>