River
River

Reputation: 63

JavaScript Dividing Array Values by an Int

I can't explain where the error is occurring here but the basic division I am attempting using array values is resulting in outrageously large numbers in the tens of thousands.

Inside of a for loop "for (var i = 0; i < 6; i ++)" I have the follow code"

var av1 = (stud[i][0][0] + stud[i][0][1] + stud[i][0][2])/3;

For clarity, there is a failure on every iteration of this loop. The initial values are as follows:

stud[0][0][0] = '77';
stud[0][0][1] = '81';
stud[0][0][2] = '85';

So I would expect an 81, plain and simple. However, the output it is coming up with is 259395. Anyone know what I'm doing wrong here?

Upvotes: 0

Views: 173

Answers (1)

beejay
beejay

Reputation: 631

Your array values are strings, not integers. As such, your addition is creating a value of 778185. After being type coerced to an integer and divided by 3, you get exactly 259395. You'll need to make sure your integers are stored as integers.

Upvotes: 2

Related Questions