javascript - is this an efficient way to parseInt? -


is best way convert string int in while loop?i think code in parseint inefficient way because makes computer parseint everytime until dowhile loop true

function randomupper(upper) {     return math.floor(math.random() * upper) + 1 }  var limit = 10; var guess; var randomnumber = randomupper(limit); var guessmade = 0;  do{     guess = prompt("guess number");     guess = parseint(guess);     guessmade++; } while (guess !== randomnumber);  document.write(guessmade) 

javascript such high level language things string integer type coercions don't put stress on language.

you can't avoid type coercion either. compare string integer properly, @ point have same type (loose comparison aside), meaning have convert manually strict comparison won't compare otherwise.

this shouldn't concern, when you're using prompt, user input function awaits feedback browser window. that's whole lot more expensive string integer conversion.


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -