Daniel Eugen
Daniel Eugen

Reputation: 2780

Integer division in JavaScript Error

When i try to divide and get a result using the following equation in Javascript

var level = (userLevel + 1)/2;

where userLevel = 7

i get 35.5 as a result instead of 4 ?

what am i doing wrong ?

Upvotes: 0

Views: 260

Answers (2)

Girish Hosamani
Girish Hosamani

Reputation: 1203

javascript will consider your userLevel as a sttring for that you have convert the type into int or parse it to int

Try this :

var level = ((userLevel-0) + 1)/2;

Cheers :-)

Upvotes: 0

Subin Sebastian
Subin Sebastian

Reputation: 10997

userLevel is a string, use

var level = (parseInt(userLevel) + 1)/2;

7+1 evaluates to 71 as 7 is a string

Upvotes: 4

Related Questions