Reputation: 551
I have 5 input fields (range sliders) and I'm trying to return a result when any of the input values changes. The problem is "value4" is used 2 times in the equation and this is causing the problem. If I change "value4" my result does not change.
function output(){
var value1 = document.getElementById('NumberOfEmployees_input').value;
var value2 = document.getElementById('AverageSalary_input').value;
var value3 = document.getElementById('AverageTime_input').value;
var value4 = document.getElementById('WorkdaysWeek_input').value;
var value5 = document.getElementById('AccountingHours_input').value;
document.getElementById('saving').innerHTML =
(((parseInt(value1) * parseInt(value2))/(parseInt(value4) * parseInt(1920))) * (parseInt(value3) * parseInt(value4) * parseInt(48)))
+ (parseInt(value5) * parseInt(13500)) - parseInt(183600);
}
Can anybody give me a hint? Thank you!
Upvotes: 0
Views: 2039
Reputation: 8997
Here is your equation
(
(
(parseInt(value1) * parseInt(value2))
/
(parseInt(value4) * parseInt(1920))
) *
(parseInt(value3) * parseInt(value4) * parseInt(48))
)
+ (parseInt(value5) * parseInt(13500))
- parseInt(183600);
v1 • v2
(----------- • v3 • v4 • 48) + (v5 • 13500) - 183600
v4 • 1920
// valueN has been substituted with vN
By observation, v4
cancels out.
This reduces your chain of math to
( (parseInt(value1)*parseInt(value2)*parseInt(value3)*48) / 1920 )
+ (parseInt(value5) * 13500)
- 183600;
Note that originally, v4
= 0 would fail. This may or may not be intended.
To make your code more readable you can separate the numerator and denominator as so
n = parseInt(value1) * parseInt(value2) * parseInt(value3) * 48
document.getElementById('saving').innerHTML = (n/1920) + (v5 * 13500) - 183600;
While this might not answer your question of using 2 input values, this provides a workaround in your situation. You didn't seem to realise this when you posted your question. Perhaps you misplaced a bracket or made an error in your calculations?
Upvotes: 1
Reputation: 12737
The code is very difficult to read. I suggest simplifying the code by first, shortening the variable names. Then apply parseInt
at an early stage to reduce clutter afterwards.
Plus, there is no need to parseInt(123)
since 123
is already Integer.
function output(){
var v1 = parseInt(document.getElementById('NumberOfEmployees_input').value);
var v2 = parseInt(document.getElementById('AverageSalary_input').value);
var v3 = parseInt(document.getElementById('AverageTime_input').value);
var v4 = parseInt(document.getElementById('WorkdaysWeek_input').value);
var v5 = parseInt(document.getElementById('AccountingHours_input').value);
var n = v1 * v2; // nominator
var d = v4 * 1920 * v3 * v4 * 48; // denominator
document.getElementById('saving').innerHTML = (n/d) + (v5 * 13500) - 183600;
}
Upvotes: 1
Reputation: 1913
Your equation can be simplified, you added too many brackets (I removed the parseInt()
function to make it more readable).
It is equivalent to:
value4
is canceled:
You may have made an error in your equation.
Upvotes: 2