Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by rgranlund
at 2024-07-29 10:59:53
Point:500 Replies:6 POST_ID:829153USER_ID:11963
Topic:
JavaScript;;jQuery
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> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:
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.
Expert: Ray Paseur replied at 2024-08-01 12:11:51
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?
What was wrong with the answer? What were you expecting that you didn't get?
Expert: Ray Paseur replied at 2024-07-29 13:08:24
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. Accepted Solution
Expert: Ray Paseur replied at 2024-07-29 13:06:19
500 points GOOD
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.
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;} 1:2:3:4:5:6:7:
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
http://www.w3schools.com/js/js_operators.asp
Author: rgranlund replied at 2024-07-29 13:05:36
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> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:
Expert: duncanb7 replied at 2024-07-29 12:27:24
Try to use eval() on javascript mentioned at this site , http://www.w3schools.com/jsref/jsref_eval.asp
Duncan
Duncan
Expert: Easwaran Paramasivam replied at 2024-07-29 11:23:22
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.
var sum = parseInt(my_input1) + parseInt(my_input2);
Like Parsing methods for float and date are available. Please use as per your need.