PDA

View Full Version : Add two values, not two numbers



Nate_Bro
09-05-2004, 12:13 AM
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>

cr3ative
09-05-2004, 07:46 AM
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

Nate_Bro
09-05-2004, 05:21 PM
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);

cr3ative
09-05-2004, 05:49 PM
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

Nate_Bro
09-06-2004, 12:39 AM
Thanks, for the explanation, and i already replaced 'summing' with 'number'.