Reputation: 14145
if I need to calculate something using certain formula with dynamic values. Problem is that this formula is changing based on represented value, for example I do not want to include elements with zero value inside formula.
var one = $('#one').val();
var two= $('#two').val();
var three= $('#three').val();
var four= $('#four').val();
...
var ten= $('#ten').val();
formula is like
(one * two) + (three * 4) + (five * 5) + ...
there are many cases I should be aware of. If three is NaN
or 0
I should skip that part, and same for all other elements.
How would you do this?
Upvotes: 0
Views: 94
Reputation: 899
I took a sample html and assumed what are you trying to do.
I have created a
fiddle
$(document).on('click', '.calculate', function(){
var formula = '';
var value = 0;
$('.calcualte_field').each(function(i,j){
++i
var val = $(this).val();
// + put your condition here
if(!isNaN(val) && val){
formula +='('+val+' * '+i+') +';
value +=(i * val);
}
});
alert(value);
$(".result").text("Your formula is "+formula +"\n result is :=" +value);
})
it may help you. Where I just put the condition, you can add your conditions
Upvotes: 0
Reputation: 1586
when the value is set to 0 it would "ignore" that part because 0*X=0 and a number plus 0 is still the number? so ignored? therefore you can give every of your number elements in the html a class you wish in my case its ".numbers". and loop through them, make an object and give every not integers the value 0... then integrate that new generated object in your formula. beside that you can change the value the numbers which are NaN should get before the function.
with this way you can declare all the elements with a loop and dont have todo this:
var one = $('#one').val();
var two= $('#two').val();
var three= $('#three').val();
var four= $('#four').val();
...
didn't test it but it should do the job.
var alternativeNumber = 0;
$('.numbers').each(function(alternativeNumber){
var id = $this.attr('id');
var val = '';
var numberObj = {};
if (isNaN($($this).val())) {
val = alternativeNumber;
} else {
val = $this.val();
}
numberObj[id] = val;
return numberObj;
});
(numberObj.one * numberObj.two) + (numberObj.three * 4) + (numberObj.five * 5) + ...
Upvotes: 1
Reputation: 71
first of all - add a class to your divs like to_calculate
than you can add an event to this class:
$('.to_calculate').on('change', function(){
if(parseInt($(this).val()) != 'NaN'){
make_calculations()
}
})
and of course put your calculations into make_calculations
function
Upvotes: 0
Reputation: 16
I think the real deal here is when one of the variable is not numeric.
// example
var one = isNaN($('#one').val()) ? 0 : $('#one').val();
Upvotes: 0
Reputation: 377
I would break the formula into pieces, giving each one their own method, then plugging those methods into the formula. Within those methods, I would check for 0 or NaN values, and return the desired result. For instance, in your above example, if three is 0 or NaN, I would return 4 from its method part. If it is a value you can use, then I would return three * 4, as desired.
You could use that same method for five * 5. And so on. If your method is always some number times another number, or return whichever is not zero, just have it accept two integers and pass in the desired values or variables.
Something like:
MultiplyOrReturnWhicheverIsNotZero(num1, num2){
if (num1 === NaN || num1 === 0) return num2;
if (num2 === NaN || num2 === 0) return num1;
return num1 * num2
}
Upvotes: 1