how to avoid nan in javascript