What I thought this code would do is, if i typed non-numbers it would continue prompting me until I finally input a number which it then will output the word "one" on the page.
What it does is if I type a number first it outputs the word "one" on the page. If i type a non-number it continuously loops even if I put a number afterwards.

And this is a learning experiment. So please be nice

<script type="text/javascript">
var luckynumber = prompt("Whats the number?");
luckynumber = parseInt(luckynumber);
	while(isNaN(luckynumber)) {
		prompt("Numbers only!")
	}
	if (! isNaN(luckynumber)) {
		document.write('one')
	}
</script>

Recommended Answers

All 3 Replies

Member Avatar for stbuchok

Change your while loop to the following:

while(isNaN(luckynumber)) {
	luckynumber = prompt("Numbers only!");
}

Also, please remember inserting semicolons at the end of your statements.

Change your while loop to the following:

while(isNaN(luckynumber)) {
	luckynumber = prompt("Numbers only!");
}

Also, please remember inserting semicolons at the end of your statements.

Thank you. I think I understand it a little better now. I was also unaware that "luckynumber = prompt("Numbers only!");" was an end of a statement.

I didnt want to make another post.
is there a difference between + and , ?
like in these 2 codes

document.write('hello' + 'bye');
document.write('hello' , 'bye');
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.