Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: prompt help

  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    prompt help

    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.

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,035
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    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.

    Code:
    <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
    Last edited by Philip M; 10-15-2008 at 08:25 AM.

  • #5
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •