Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Tomple

macrumors 6502a
Original poster
Dec 12, 2008
604
0
New York, New York
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
 
Last edited by a moderator:
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.
 
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.
 
Last edited:
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.

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 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.

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?
Screen_shot_2011-01-29_at_9.09.41_AM.png


Any other tips for a beginner?

Thanks!
 
This was my first script, and I thought the separate line barces looked nice :) any reason not to use them? Thanks!

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.
 
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.

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
 
Any other tips for a beginner?

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
 
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

Cool thanks!
 
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>
 
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.
 
Last edited:
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.

Wow thanks! Thanks for the debugging info as well! What are you using to debug, just your standerd browser debugger?

Thanks a lot!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.