/    Sign up×
Community /Pin to ProfileBookmark

help need with basic program

Cant work out where im going wrong with this basic program. It is suppose to prompt the user for a number between 1 and 20, continue to display their number if it is within the values, if not prompt them again, then display their chosen number.

<HTML>
<HEAD>
<title>New Page 1</title>

<SCRIPT LANGUAGE = “JavaScript”>
var userNumber;
var yourNumber;

userNumber = window.prompt (‘please enter a number in the range 1 to 20’,”);
userNumber – parseFloat(userNumber);
while (userNumber <1 || userNumber >=20)
{
alert(‘Please re-enter number should be in range 1 to 20’ + ‘<BR>’);
userNumber = window.prompt (‘please enter a number in the range 1 to 20’,”);
userNumber = parseFloat(yourNumber)
};
userNumber = yourNumber
document.write(‘The user’s chosen number was ‘ + ‘yourNumber’)

</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>

many thanks

Rob

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterMar 14.2006 — Add a string escape backslash to the single-quote character and remove the quotes from the variable to the right of the string-concentration addition operator character.
<i>
</i>document.write('The user's chosen number was ' + yourNumber);
Copy linkTweet thisAlerts:
@airwolfdirectukauthorMar 14.2006 — many thanks for that. I now get the following after running the program:

The user's chosen number was undefined

any idea why the number i put in does not get displayed?
Copy linkTweet thisAlerts:
@UltimaterMar 14.2006 — As far as your actual algorithm, you'd best rewrite it like this:
<i>
</i>&lt;SCRIPT TYPE="TEXT/JAVASCRIPT"&gt;
var userNumber;
var yourNumber;

userNumber = window.prompt ('please enter a number in the range 1 to 20','');
userNumber = parseFloat(userNumber);
while (userNumber &lt;1 || userNumber &gt;=20)
{
alert('Please re-enter number should be in range 1 to 20' + 'n');
userNumber = window.prompt ('please enter a number in the range 1 to 20','');
userNumber = parseFloat(userNumber);
};
yourNumber=userNumber;
document.write("The user's chosen number was " + yourNumber);

&lt;/SCRIPT&gt;
×

Success!

Help @airwolfdirectuk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...