Results 1 to 4 of 4

Thread: Java Script Addition Problem

  1. #1
    Join Date
    May 2009
    Posts
    59

    Java Script Addition Problem

    I am a newbie in javascript. I have some issue in simple addition program. I was making a simple program to add numbers. the first part is hat it prompts asking that how may do you want to add. This one is fine. But in the second part. When I put the numbers like 3 + 3 + 3. It gives the result 333 instead of 9. How can I correct this. Second problem. I was trying to add two numbers in a window froma a text box inside a frame. The issue is that as they are strings so whenever i use a plus sign they build strings. The below is an example of what I am doing.
    value1=window.parent.SelectSquad.myballroom1_value.value;
    value2=window.parent.SelectSquad.myballroom2_value.value;
    total=value1 + value2;
    window.parent.SelectSquad.spent.value=total;

    Now how to work out with this issues.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Java Script Addition Proplem

    Javascript is a very easy in handling stings and numbers. You correct your addition problem you can do two things. First take care that the addition is handled as numbers or the input values are handled as numbers. I am giving solution for your first problem i.e the addition one. I will go with the first option because it is easy. But putting the multiplication sign Java treat it as numbers. Follow the below codec's
    PHP Code:
    var num1 prompt("Which number would you like to add? ","");
    num1 num1 1;
    sum += num1

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Java Script Addition Proplem

    For the last problem you mentioned I had some solution for that. Look into my suggestion below and make some correction. The correction will give you a better result as compare to before. You will now need to look at the parseInt.
    Code:
    total=parseInt(value1) + parseInt(value2);
    There is a different function in Javascript for the same problem It is called parseFloat function.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Java Script Addition Proplem

    I am listing a piece of codec sample created on your query for your problem. Just copy paste or better refer it. Then you will came to know where is the problem. Try the below codec's.

    PHP Code:
    function dispSpent() {
    var 
    currForm window.parent.SelectSquad;
    value1 currForm.myballroom1_value.value;
    value2 currForm.myballroom2_value.value;
    if (
    isNaN(value1) || parseInt(value1) != value1) {
    alert('Please enter an integer (whole number).');currForm.goalkeeper1_value.focus();currForm.myballroom1_value.select();return false;
    }
    if (
    isNaN(value2) || parseInt(value2) != value2) {
    alert('Please enter an integer (whole number).');currForm.myballroom2_value.focus();currForm.myballroom2_value.select();return false;
    }
    total = eval(value1) + eval(value2);
    currForm.spent.value total;

    Best of luck.

Similar Threads

  1. Is there any future for java script?
    By Gag$nesh in forum Software Development
    Replies: 4
    Last Post: 28-05-2011, 07:58 PM
  2. java script code for addition of two numbers
    By Sonam Goenka in forum Software Development
    Replies: 6
    Last Post: 05-01-2010, 10:39 AM
  3. can someone look at this script? JAVA
    By Daren in forum Software Development
    Replies: 2
    Last Post: 07-03-2009, 08:26 PM
  4. Shell script Problem in JAVA programing
    By Pandya in forum Software Development
    Replies: 3
    Last Post: 04-03-2009, 08:26 AM
  5. Replies: 2
    Last Post: 14-01-2009, 01:25 PM

Tags for this Thread

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
  •  
Page generated in 1,714,033,607.15946 seconds with 17 queries