Calculate distance in JavaScript