add an isNaN check to the while loop