Helpful Information
 
 
Category: General Coding
Add two values, not two numbers

Ok, here is my script.

My problem is that i cant figured how to get the addition to work, if you add 2+2 it writes 22 instead of 4, if you can, please help, thanks.

I Put // where the problem is.

<Head>
<CENTER>

<FORM NAME="number2">
<INPUT TYPE="text" Value="0" NAME="numb4">
</FORM>

<FORM NAME="number">
<INPUT TYPE="text" Value="" NAME="num1">
<INPUT TYPE="text" Value="" NAME="num2">
<INPUT TYPE="button" NAME="none56" Value=" C " OnClick="reset(); add(); go()">
</FORM>

</HEAD>
<body>
<SCRIPT><!--
function go(){
document.number.num1.value = ("");
document.number2.numb4.value = ("0");
}

var numb1 = document.number.num1.value
var numb2 = document.number.num2.value

function reset(){
document.number2.numb4.value = ("0");
document.number.num1.value = ("");
document.number.num2.value = ("");
}

// Here is the Problem

function add(){
document.number2.numb4.value = (document.number.num1.value + document.number.num2.value);
document.number.num1.value = (document.number.num1.value + document.number.num2.value);
document.number.num2.value = ("");
}

function mult(){
document.number2.numb4.value = (document.number.num1.value * document.number.num2.value);
document.number.num1.value = (document.number.num1.value * document.number.num2.value);
document.number.num2.value = ("");
}

function div(){
document.number2.numb4.value = (document.number.num1.value / document.number.num2.value);
document.number.num1.value = (document.number.num1.value / document.number.num2.value);
document.number.num2.value = ("");
}

function sub(){
document.number2.numb4.value = (document.number.num1.value - document.number.num2.value);
document.number.num1.value = (document.number.num1.value - document.number.num2.value);
document.number.num2.value = ("");
}
//-->
</SCRIPT>

<FORM NAME="MyForm">
<INPUT TYPE="button" NAME="none" Value=" + " OnClick="add()">
<INPUT TYPE="button" NAME="none9" Value=" ÷ " OnClick="div()">
<INPUT TYPE="button" NAME="none5" Value=" - " OnClick="sub()">
<INPUT TYPE="button" NAME="none3" Value=" X " OnClick="mult()">
</FORM>
</CENTER>
</body>

Replace your problematic lines with these instead:




document.number2.numb4.value = parseInt(document.number.num1.value) + parseInt(document.number.num2.value);
document.number.num1.value = parseInt(document.number.num1.value) + parseInt(document.number.num2.value);



Hope this helps
cr3ative

Thanks, this code worked!!


document.number2.numb4.value = parseInt(document.number.num1.value) + parseInt(document.number.num2.value);
document.number.num1.value = parseInt(document.number.num1.value) + parseInt(document.number.num2.value);

Yes, and it works because it specifically idenfifies the values as integers (numbers). Otherwise, javascript can add two STRINGS together in other contexts, as you experienced.

For example when I program using VB.net, I use value1 + value2 to create my program title - it adds the version string to the program name. Your script was simply adding two 'text' values together. Specifying the values as integers solves this.

edited original code post: replaced 'summing' with 'number', my mistake.

cr3ative

Thanks, for the explanation, and i already replaced 'summing' with 'number'.










privacy (GDPR)