This is probably a question with an really logical answer.. But I really don't understand this!!
Why, does this give different results..
Only difference is a for loop and a while loop.. Even while they loop exactly as many times???
array = [1.2344, 2.47373, 3.444];
var total = 0,
total2 = 0,
i = array.length,
whileLoops = 0,
forLoops = 0;
while (i--) {
whileLoops++;
total += array[i];
}
for (var i = 0, len = array.length; i < len; i++) {
forLoops++;
total2 += array[i];
}
if (total !== total2) {
console.log("BOE")
}
I tried parseFloat, but this also wasn't helping :(
It it because the Javascript engine rounds numbers in a sort of way???
On request: the fiddle http://jsfiddle.net/5dsx0ump/
UPDATE
Would the solution be to first to a * 1000 and after all the calculations, divide again by 1000, to keep round numbers?