...

View Full Version : prompt help



snefmoo
10-13-2008, 05:24 PM
i need help with the following task.

Write a program that begins by prompting the user to type in a positive integer (lets call it n). If the number typed (n) is not positive (i.e. bigger than zero) than it should ask again. After this the program should repeatedly ask the user to type in an integer, until 5 numbers have been typed in. After this the program should print the sum of these numbers.

i have tried using other code source sites to try it myself but i am a complete beginner to prompts (and only know a little about javascript) and cannot manage to get anything working.
any help would be appreciated.

A1ien51
10-13-2008, 06:08 PM
Sounds like homework...

Show us what you have tried and we should be able to point you where your problems are.

hopefully no one gives you the answer without you doing the work.

Eric

snefmoo
10-14-2008, 11:44 PM
here is what i managed to come up with from looking at a similar example that my tutor gave me. the one he did works fine but the code i have come up with just returns a blank window.

<html>
<head>
<title>Addition</title>
</head>
<body>
<script LANGUAGE="JavaScript">
num1 = prompt("please enter a number");
if (num1<0)
{
alert("the number must be positive")
submitOK="false"
}

</script>

</body>
</html>



</BODY>
</HTML>

Philip M
10-15-2008, 08:08 AM
It is slightly disconcerting that your tutor offers <script LANGUAGE="JavaScript"> which is long deprecated.


Look at this and learn from it. Remember that you cannot expect people to do your homework for you every time.


<html>
<head>
<title>Addition</title>
</head>
<body>
<script type = "text/javascript">
var submitOK = true; // declare and define submitOK - true not "true"
num1 = prompt("Please enter a number",""); // provide a default, in this case blank
if (isNaN(num1) || (num1<0)) { // check for non-numeric or negative entry
alert ("The number must be positive");
submitOK = false; // not "false" in quotes which is a literal
}
alert (submitOK);

</script>

</body>
</html>

You will need to add a for loop to repeat the addtion five times and another variable named sum to hold the result. Remember that the value entered by the user is a string until changed to a number, so you want 3+3+3+3+3 to equal 15 and not 33333. Hint - use parseFloat()



Stolen Painting Found by Tree - newspaper headline

snefmoo
10-15-2008, 09:47 AM
thankyou for the help. its really appreciated.
i wasnt expecting anyone to do my homework for me, if i did i could have just gone to one of my friends and copied theirs.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum