Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
142
rated 0 times [  144] [ 2]  / answers: 1 / hits: 28404  / 13 Years ago, mon, november 7, 2011, 12:00:00

Let's say I have some numbers that I want to multiply and add.



var a = 5;
var b = 10;
var c = 2;
var d = 3;


I want to sum b and c then multiply that by a, then add d. Easy right?



If if were a normal equation the formula would look like: (a * (b + c) + d)



But how do I do that in JQuery?



(Note: the reason for JQuery is that I'll be getting these numbers from fields and divs... and placing a total elsewhere, etc.)


More From » jquery

 Answers
74

By default script language does not know type as int or float. So you can fix that by multiplying 1 to the value you expect to be a number.



var a = 5;
var b = 10;
var c = 2;
var d = 3;

var total = a*1 * (b*1 + c*1) + d*1;

[#89262] Saturday, November 5, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tonisandyp

Total Points: 694
Total Questions: 97
Total Answers: 77

Location: British Indian Ocean Territory
Member since Tue, Feb 22, 2022
2 Years ago
;