Results 1 to 5 of 5

Thread: Add two values, not two numbers

  1. #1
    Join Date
    Sep 2004
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation 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>

  2. #2
    Join Date
    Aug 2004
    Location
    Brighton
    Posts
    1,563
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Replace your problematic lines with these instead:

    Code:
    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
    Last edited by cr3ative; 09-05-2004 at 05:57 PM. Reason: summing value incorrect
    A retired member, drop me a line through my site if you'd like to find me!
    cr3ative media | read the stickies

  3. #3
    Join Date
    Sep 2004
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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);

  4. #4
    Join Date
    Aug 2004
    Location
    Brighton
    Posts
    1,563
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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
    Last edited by cr3ative; 09-05-2004 at 05:59 PM.
    A retired member, drop me a line through my site if you'd like to find me!
    cr3ative media | read the stickies

  5. #5
    Join Date
    Sep 2004
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •