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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    trying to figure out what I am doing wrong

    Code:
    <!DOCTYPE html>
    <html>
    
    <head>
    	<script>
    		//this is the syntax to create a function.
    		function myJavaScriptCode()
    		{
    			//This is the syntax to declare a variable.
    			var myName;
    			var displayName;
    			//Assign the variable a value.
    			myName = window.prompt("Please enter your name","Harry Potter");
    			displayName = "Hello " + myName;
    
    			//Display the value based on whatever conditions you like.
    			if (myName > 4)
      {
      window.alert("You entered a number larger than 4. Please try again!!!";
      }
      elseif (myName < 1)
      {
      window.alert("You entered a number smaller than 1. Please try again!!!;
      }
      else
      {
      				  window.alert(displayName);
    					
    		}
    		
    	</script>
    </head>
    
    <body>
    	<button onclick="myJavaScriptCode()">Do it</button>
    </body>
    
    </html>

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,148
    Thanks
    39
    Thanked 506 Times in 500 Posts

    Lightbulb

    You have a number of errors in your original code.
    Look at the error console if you are using the FF or Chrome browsers.

    For example:
    1. Missing closing ')' around alert messages
    2. Non-matching { } pairs
    3. window.alert is not required. Use just alert only

    Compare this to your code, line-by-line and character-by-character.
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script>
    //this is the syntax to create a function.
    function myJavaScriptCode() {
    //This is the syntax to declare a variable.
      var myName;
      var displayName;
    //Assign the variable a value.
      myName = prompt("Please enter your name","Harry Potter");
      displayName = "Hello " + myName;
    
    //Display the value based on whatever conditions you like.
      if (myName > 4)   {
        alert("You entered a number larger than 4. Please try again!!!"); 
      } else { 
        if (myName < 1) {
          alert("You entered a number smaller than 1. Please try again!!!");
        } else { alert(displayName); }
      }
    }		
    </script>
    </head>
    <body>
    <button onclick="myJavaScriptCode()">Do it</button>
    </body>
    </html>


  •  

    Posting Permissions

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