I have to calculate sales percentage between two variables and I was wondering how to do that with jQuery.
I would use the following formula to get my sales percentage
(40-30)/40 * 100 = 25%.
Inputting my parseFloated variables would look like the following
(tiLine - tiSale) / tiLine * 100 = fixedTo [1]('%')
What is the proper syntax for an equation like this?
jQuery syntax follows the same syntax as JavaScript. jQuery is basically JavaScript on steroids. So for the most part you have the answer already. Just create a helper function to take get the sales percentage.
Your syntax would have to go something like this:
function salesPcnt(tiLine, tiSale) {
var pcnt = ((tiLine - tiSale)/tiLine) * 100;
return pcnt.toFixed[2] + '%';
}
What this function does is returns the percentage in string form. So now, all you would need to do is call the function and use the return value properly.
ie. Usage for jQuery
$(document).ready(function() {
var tiLine = 40;
var tiSale = 30;
console.log(salesPcnt(tiLine, siSale)); //will print 25.00%
});