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 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Prompt boxes with JavaScript

    I am trying to get these prompt boxes to display for age and resting heart rate. I cannot get this to work...This is what I have...any suggestions?

    <html>
    <head>

    <h1>Calculate Your Target Heart Rate</h1>

    <p>You can calculate your heart rate so that you can get the maximun results from your cardiovascular workout.
    Just follow these simple steps:</p>

    <script type="text/javascript">
    function show_prompt()
    {
    var number = prompt ("Please enter your Resting heart rate:","Enter Rest Heart Rate Here!");
    if (number! null && number! = "")
    var age = prompt ("Please enter your Age", "Enter Your Age Here!");

    {
    document.write ("Your Resting Heart Rate is" + number);
    document.write ("Your Age is" + Age);
    }
    }

    </script>
    </head>
    <body>

    <input type ="button" onclick = "show_prompt ()" value = "Start Calculating Here!"/>


    </body>
    </html>

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by newbanks View Post
    any suggestions?
    Yes - use the error console.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,899
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Have a look at the position ofyour braces { and }

    When posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.


    Code:
    <html>
    <head>
    
    <h1>Calculate Your Target Heart Rate</h1>
    
    <p>You can calculate your heart rate so that you can get the maximun results from your cardiovascular workout.
    Just follow these simple steps:</p>
    
    <script type="text/javascript">
    function show_prompt() {
    for (var i=1; i<2; i++) {
    var h = parseInt(prompt ("Please enter your Resting heart rate:","Enter Rest Heart Rate Here!"));
    if ((isNaN(h)) || (h == "")) {
    alert ("You must enter a number!!  ");
    i -- ;
    }
    }
    
    for (var i=1; i<2; i++) {
    var age = parseInt(prompt ("Please enter your Age", "Enter Your Age Here!"));
    if ((isNaN(age)) || (age == "")) {
    alert ("You must enter a number!!  ");
    i -- ;
    }
    }
    
    document.write ("Your Resting Heart Rate is " + h + "<br>");
    document.write ("Your Age is " + age);
    }
    
    </script>
    </head>
    <body>
    
    <input type ="button" onclick = "show_prompt ()" value = "Start Calculating Here!"/>
    
    
    </body>
    </html>
    Note that document.write() is long obsolete. Also that Javascript is case-sensitive and Age != age.

    It is not very sensible to name a Javascript variable "number".

    Although the user must enter numbers, there is no check that the numbers are sensible.

    "Life is like a mirror, we get the best results when we smile at it. " - Anonymous
    Last edited by Philip M; 01-23-2012 at 06:58 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,593
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Using prompt() to collect data is also not advisable as some browsers will display an extra option to either turn off further dialogs or to turn off Javascript completely as the dialogs built into JavaScript are now intended only to be used for debugging purposes.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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