javascript - How to stop a while loop after looping x times -


while(vowels != "no" && vowels != "no" && vowels != "stop") {     vowels = prompt("enter vowel.");      if (vowels != "a" && vowels != "a" && vowels != "e" && vowels != "e"         && vowels != "i" && vowels != "i" && vowels != "o" && vowels != "o"         && vowels != "u" && vowels != "u")     {         score+=-1;     }     else     {         score+=+1;     } } 

how make stop after 5 times?

in while conditional statement add:

var = 0; while(vowels != "no" && vowels != "no" && vowels != "stop" && < 5) {    ...    i++; } 

this add tick counter loop. use for loop.

for loop example:

for (var = 0; < 5; i++) {     if (vowels != "no" && vowels != "no" && vowels != "stop")        break;     vowels = prompt("enter vowel.");      if (vowels != "a" && vowels != "a" && vowels != "e" && vowels != "e"         && vowels != "i" && vowels != "i" && vowels != "o" && vowels != "o"         && vowels != "u" && vowels != "u")     {         score+=-1;     }     else     {         score+=+1;     } } 

also, a classic example of tick counter. :)

edit

as @drax pointed out, write for as:

for (var = 0; < 5 && vowels != "no" && vowels != "no" && vowels != "stop"; i++) {     vowels = prompt("enter vowel.");      if (vowels != "a" && vowels != "a" && vowels != "e" && vowels != "e"         && vowels != "i" && vowels != "i" && vowels != "o" && vowels != "o"         && vowels != "u" && vowels != "u")     {         score+=-1;     }     else     {         score+=+1;     } } 

this modifies conditional of for.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -