Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 01-06-2009
Member
 
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.
Reply With Quote
  #2  
Old 01-06-2009
Member
 
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
Reply With Quote
  #3  
Old 01-06-2009
Member
 
Join Date: May 2008
Posts: 2,008
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.
Reply With Quote
  #4  
Old 01-06-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Java Script Addition Problem"
Thread Thread Starter Forum Replies Last Post
Is there any future for java script? Gag$nesh Software Development 4 28-05-2011 07:58 PM
java script code for addition of two numbers Sonam Goenka Software Development 6 05-01-2010 10:39 AM
can someone look at this script? JAVA Daren Software Development 2 07-03-2009 08:26 PM
Shell script Problem in JAVA programing Pandya Software Development 3 04-03-2009 08:26 AM
Problem with script in order to open another script in an extra window Samsher Software Development 2 14-01-2009 01:25 PM


All times are GMT +5.5. The time now is 03:59 AM.