Solved

Javascript Addition

Posted on 2014-07-29
6
396 Views
Last Modified: 2014-08-01
I have the following piece of Javascript that I would like to perform a multiplication and ten addition equation.  The Multiplication is working but not the addition.  When it gets to the addition part it appends the figure to the other return.
<script>
function findSum(){
	var baseOne = document.getElementById('addon-28-custom-price[custom-price]').value;
	var myBox1 = document.getElementById('addon-28-custom-price[my-deductible]').value;	
	var myBox2 = document.getElementById('addon-28-custom-price[bicycle-liability]').value;
	var myBox2a = myBox2 != '' ? myBox2 : 0;
	//var myBox3 = document.getElementById('qty3').value;

	//var myBox3a = myBox3 != '' ? myBox3 : 1;

	var result = document.getElementById('result');	
	var myResult = (baseOne) * (myBox1) + (myBox2a);

	 document.getElementById('result').value = myResult;
}
</script>

Open in new window


So at this line (var myResult = (baseOne) * (myBox1) + (myBox2a);) the answer will print out something like: 482.45.50 instead of adding 482 and 45.50 together.
0
Comment
Question by:rgranlund
6 Comments
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 40227406
You have to use parse method as given below example.

 var sum = parseInt(my_input1) + parseInt(my_input2);

Like Parsing methods for float and date are available. Please use as per your need.
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40227558
Try to use eval() on javascript  mentioned at this site , http://www.w3schools.com/jsref/jsref_eval.asp

Duncan
0
 
LVL 7

Author Comment

by:rgranlund
ID: 40227641
is the following correct?
<script>
function findSum(){
	var baseOne = document.getElementById('addon-28-custom-price[custom-price]').value;
	var myBox1 = document.getElementById('addon-28-custom-price[my-deductible]').value;	
	var myBox2 = document.getElementById('addon-28-custom-price[bicycle-liability]').value;
	var myBox2a = myBox2 != '' ? myBox2 : 0;
	
	var myBox3 = document.getElementById('addon-28-custom-price[medical-payments]').value;
	var myBox3a = myBox3 != '' ? myBox3 : 0;

	var result = document.getElementById('result');	
	var prep = (baseOne) * (myBox1);
	var myResult = prep + parseInt(myBox2a);

	 document.getElementById('result').value = myResult;
}
</script>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40227642
In JavaScript the plus sign is an overloaded operator.  It is used to mean both addition and string concatenation.  This was a stupid mistake, but when you consider that javaScript was created in about two months time, it's easy to understand the mistake.

You can learn JavaScript at Codeavengers.com, and I recommend that you try it out.  My students loved it!

You might try using parseFloat. The input variables will be converted to floats, which will work whether the string contains an integer or a float. Your result will always be a float.  But there is always the possibility of turning up NaN, which means "Not a Number."  I have not tested this script but it might be worth trying.

function alwaysAddAsNumbers(numberOne, numberTwo){
  var parseOne = parseFloat(numberOne),
      parseTwo = parseFloat(numberTwo);
  if (isNaN(parseOne)) parseOne = 0;
  if (isNaN(parseTwo)) parseTwo = 0;
  return parseOne + parseTwo;
}

Open in new window

See also the note here, "If you add a number and a string, the result will be a string!":
http://www.w3schools.com/js/js_operators.asp
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40227648
Is the following correct?
We can't tell.  We would need to see the HTML for the form, and we would need to know what data the client typed into the form.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40235091
Please provide an explanation of the marked-down grade.  To quote from the E-E grading guidelines, "B grade means the solution given lacked some information or required you to do a good amount of extra work to resolve the problem. When closing the question, the asker should explain why a B grade was awarded.

What was wrong with the answer?  What were you expecting that you didn't get?
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with mod_substitute 18 44
HTML auto refresh to another site 4 30
Hta File displays dynamic File names 4 50
Diff of the day 2 10
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question