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
Like Tree1Likes
  • 1 Post By Philip M

Thread: Needing a bit of help with variables for a greeting dialog

  1. #1
    New Coder
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Needing a bit of help with variables for a greeting dialog

    Needing to create a greeting.html to get two names (prompt twice) from the user. Then create a function called Greet that is passed two parameters, who1 and who2 (the two names you received) and include both names in a single greeting dialog.
    Attached Files Attached Files

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by Krunch View Post
    Needing to create a greeting.html to get two names (prompt twice) from the user. Then create a function called Greet that is passed two parameters, who1 and who2 (the two names you received) and include both names in a single greeting dialog.
    We don't normally do homework, but as you have made an effort .....

    Code:
    <!DOCTYPE html>
      <head>
        <title>      Assignment 2   </title>
      </head>
    
      
      <body style="margin-left: auto; margin-right: auto; text-align: center; font-family: Quadranta; font-size: 22px; margin-top: 2%; color: rgb(148, 148, 148);">
    
        
    <script type="text/javascript">
    var user_name = prompt ("Enter your first name in the box below","");
    var last_name = prompt ("Enter your last name in the box below","");
    	
    greet (user_name, last_name);  // call the greet() function
    
    function greet (who1, who2) {
    document.write("Hello " + who1 + " " + who2  + ", Welcome to my Site!");
    }
    </script>
        
        
      </body>
    </html>
    The greet() function is of course not necessary.
    It would be better logically if you named the first variable first_name rather than user_name.
    Be aware that prompts, alerts and document.write() are long obsolete and should be used only for testing purposes. I do understand that you are only at a very early stage in your Javascript learning.
    You have correctly identified that the script should be placed in the <body>, right ahead of the </body> tag.

    If you want something a little more sophisticated which requires the user to enter their name (although here they can defeat that with spaces):-


    Code:
    <script type="text/javascript">
    
    for (var i=1; i<2; i++) {
    var user_name = prompt ("Enter your first name in the box below","");
    if ((user_name == null ) || (user_name == "")) {
    alert ("You must enter your first name!");
    i--
    }
    }
    
    for (var i=1; i<2; i++) {
    var last_name = prompt ("Enter your last name in the box below","");
    if ((last_name == null ) || (last_name == "")) {
    alert ("You must enter your last name!");
    i--
    }
    }
    
    greet (user_name, last_name);  // call the greet() function
    
    function greet (who1, who2) {
    document.write("Hello " + who1 + " " + who2  + ", Welcome to my Site!");
    }
    </script>
    In the real world a proper name may contain only alphabetic letters, hyphen and/or apostrophe. So additional checks are required. The first letter of a proper name is capitalised.

    Quizmaster: What is 80 percent of 200?
    Contestant: Four.
    Last edited by Philip M; 06-07-2014 at 08:15 AM.
    Krunch likes this.

    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.

  • #3
    New Coder
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Much thanks for the enlightenment, feeling a bit silly as you have made it seem so simple, I have learned greatly from this little lesson, appreciate it tons, Thanks Philip.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by Krunch View Post
    Much thanks for the enlightenment, feeling a bit silly as you have made it seem so simple, I have learned greatly from this little lesson, appreciate it tons, Thanks Philip.
    Great, if you learn from it then it has been a pleasure to help you!

    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.


  •  

    Tags for this Thread

    Posting Permissions

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