Why won't my simple Javascript file run

Discussion in 'Web Design and Development' started by Tomple, Jan 28, 2011.

  1. Tomple, Jan 28, 2011
    Last edited by a moderator: Jan 29, 2011

    Tomple macrumors 6502a

    Tomple

    Joined:
    Dec 12, 2008
    Location:
    New York, New York
    #1
    I am completely at a loss for why this simple (and stupid) quiz game won't run

    Here is the javascript text

    Code:
    <html>
    <head>
    	<title>Atlas Test Script</title>
    	<script type="text/javascript">
    	
    		perfectNumber = 2;
    		
    		function checkSlices(numSlices)
    		{
    			if (numSlices == perfectNumber)
    			{
    				alert("Congratulations, You got it! ");
    			}
    			else if (numSlices > perfectNumber)
    			{
    				alert("Too Many!");
    			}
    			else if (numSlices < perfectNumber);
    			{
    				alert("Too Few!");
    			}
    			else
    			{
    				alert("You're Crazy!");
    			}
    		}
    		
    	</script>
    </head>
    <body>
    	<h3>How many slices of pizza did you eat?</h3>
    	<form method="POST" name="slicesForm" onSubmit="checkSlices(document.slicesForm.numSlices.value);">
    		<input type="text" name="numSlices" id="numSlices" />
    		<input type="Submit" name="Submit" />
    	</form>
    </body>
    </html>

    Also here is the HTML file, for those who want it http://cl.ly/4GeG
     
  2. larkost macrumors 6502a

    Joined:
    Oct 13, 2007
    #2
    The problem is you have an extra ';' in there, namely on this line:
    Code:
    else if (numSlices < perfectNumber);
    And please use the code blocks to put in your code, at least it would keep the indentation. I happen to dislike your style of putting the braces on their own lines, but at least it would be readable with indentation.
     
  3. ulbador, Jan 28, 2011
    Last edited: Jan 28, 2011

    ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #3
    I hate to say it, but you have a few VERY basic javascript errors

    The very first thing you need is to turn on javascript debugging. At least in this case, it would tell you exactly where your problem is.
     
  4. Tomple thread starter macrumors 6502a

    Tomple

    Joined:
    Dec 12, 2008
    Location:
    New York, New York
    #4
    Thanks! I feel so stupid for missing that!

    also,

    Sorry about not putting the code in a "code block" it's my first time posting on JavaScript forums. This was my first script, and I thought the separate line barces looked nice :) any reason not to use them? Thanks!


    I know I have some really basic errors, but it was my first attempt at a script so I'm pretty happy with it :)

    And do you mean enable this?
    [​IMG]

    Any other tips for a beginner?

    Thanks!
     
  5. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #5
    No.

    This just yet another religious war in the same vein as vi vs emacs or Mac OS X vs Windows or Ferrari vs Lamborghini.

    Choose a style you like and stick with it. Your style of putting braces on their own line is quite popular (especially amongst C programmers I have noticed) where as the braces on the same line seems to primarily be a Java thing. I noticed that Microsoft made the official coding standards of C# use the separate line for braces style as well.
     
  6. Tomple thread starter macrumors 6502a

    Tomple

    Joined:
    Dec 12, 2008
    Location:
    New York, New York
    #6
    Oh, ok cool

    I think I will be sticking with the separate line braces.

    Off topic
    I hope to one day learn C, but I can't even get down this simple Javascript :(

    Thanks for your help guys
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Go through a tutorial on using a debugger, for whatever browser you're using.

    If your browser's debugger sucks or has bugs itself, consider using a different browser for doing debugging.

    Even if your browser is fabulous, learn how to test and debug under another browser. The real-world is multi-browser, so you'd better learn how to test and debug your programs for the real-world.

    Google search terms:
    javascript debugger YourBrowserNameHere
     
  8. Tomple thread starter macrumors 6502a

    Tomple

    Joined:
    Dec 12, 2008
    Location:
    New York, New York
    #8
    Cool thanks!
     
  9. Tomple thread starter macrumors 6502a

    Tomple

    Joined:
    Dec 12, 2008
    Location:
    New York, New York
    #9
    Changed the code around, don't know why it wont work

    Code:
    <html>
    <head>
    	<title>Atlas Test Script</title>
    	<script type="text/javascript">
    		
    		perfectNumber = 2;
    		
    		function checkSlices(numSlices)
    		{
    			
    			var numSlices = document.getElementById("numSlices");
    			
    			if (numSlices == perfectNumber)
    			{
    				alert("Congratulations, You got it! ");
    			}
    			else if (numSlices > perfectNumber)
    			{
    				alert("Too Many!");
    			}
    			else if (numSlices < perfectNumber)
    			{
    				alert("Too Few!");
    			}
    			else
    			{
    				alert("You're Crazy!");
    			}
    		}
    		
    	</script>
    </head>
    <body>
    	<h3>How many slices of pizza did you eat?</h3>
    	<form method="POST" name="slicesForm" onSubmit="">
    		<input type="text" name="numSlices" id="numSlices" />
    		<input type="Submit" name="Submit" onclick="checkSlices();" />
    	</form>
    </body>
    </html>
     
  10. CANEHDN, Jan 31, 2011
    Last edited: Jan 31, 2011

    CANEHDN macrumors 6502a

    CANEHDN

    Joined:
    Dec 12, 2005
    Location:
    Eagle Mountain, UT
    #10
    Code:
    <html>
    <head>
    	<title>Atlas Test Script</title>
    	<script type="text/javascript">
    		
    		var perfectNumber = 2;
    		
    		function checkSlices() {
    			
    			var numSlices = document.getElementById("numSlices").value;
    		
    			if (numSlices == perfectNumber)
    				alert("Congratulations, You got it! ");
    			else if (numSlices > perfectNumber)
    				alert("Too Many!");
    			else if (numSlices < perfectNumber)
    				alert("Too Few!");
    			else
    				alert("You're Crazy!");
    		}
    		
    	</script>
    </head>
    <body>
    	<h3>How many slices of pizza did you eat?</h3>
    	<form method="POST" name="slicesForm" onSubmit="">
    		<input type="text" name="numSlices" id="numSlices" />
    		<input type="Submit" name="Submit" onclick="checkSlices();" />
    	</form>
    </body>
    </html>
    This will work. You forgot to add ".value" on line 10. You were creating an object of the element instead of creating a variable of the value of the text field. Also, I added var on line 6. And also, there's no need to pass a variable into the function. I removed that. Since you're using one line if statements, I removed the curlies.

    How I debugged it was alerted the perfectNumber variable and the numSlices variable. Once I saw that numSlices didn't have any value. By that I saw that you just needed to add .value to that.

    The best javascript debugger I've used is the built in one with firefox.
     
  11. Tomple thread starter macrumors 6502a

    Tomple

    Joined:
    Dec 12, 2008
    Location:
    New York, New York
    #11
    Wow thanks! Thanks for the debugging info as well! What are you using to debug, just your standerd browser debugger?

    Thanks a lot!
     
  12. CANEHDN macrumors 6502a

    CANEHDN

    Joined:
    Dec 12, 2005
    Location:
    Eagle Mountain, UT
    #12
    Yes. I usually just use the built in debugger. I mostly code with Safari but Firefox's is really good. Simple and easy to use.
     
  13. Tomple thread starter macrumors 6502a

    Tomple

    Joined:
    Dec 12, 2008
    Location:
    New York, New York
    #13
    Cool thanks :)
     
  14. Laird Knox macrumors 68000

    Joined:
    Jun 18, 2010

Share This Page