![]() |
JavaScript algorithm not working
Hello,
I've been working on a basic script that determines a net value based on four user-input values. After all the values have been stored, the operators do their work in calculating the final solution. If you notice below, the values in the comments are what the literal version of the equation is made up of; (x2-x1) + (x3-x2) + (x4-x3)= y. In reference, if the snippet n-j and the other variables were converse, the answers came out to be opposite from each other, as in the absolute or negativity value.PHP Code:
|
Ummm...I think you need to go back and take pre-algebra again.
If you do: x1 = 0; x2 = 4; x3 = 0; x4 = 0; Then you do: y = (x2-x1) + (x3-x2) + (x4-x3); you will be doing: y = (4 - 0) + (0 - 4) + (0 - 0) which is y = (4) + (-4) + 0 which is, correctly: y = 0 ********* In any case, just going one step further into elementary algebra, why not SIMPLIFY this equation: y = (x2-x1) + (x3-x2) + (x4-x3); which becomes: y = x2-x1+x3-x2+x4-x3; which becomes y = x2-x2 + x3-x3 - x1 + x4 which becomes y = 0 + 0 - x1 + x4 which becomes y = x4 - x1 In other words, the values entered for x2 and x3 *DO NOT MATTER*. The end result will only depend on x1 and x4. Me thinks you have much much bigger problems than just your JavaScript. Which is actually working correctly. |
| All times are GMT +1. The time now is 08:59 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.