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

danewithoutwax

macrumors newbie
Original poster
Aug 19, 2010
15
0
I'm trying to build this little piece of javascript, but it won't build it for me. Does anyone know what might be the problem? I'm still fairly new to javascript, so i hope you can bear over with me, if this is a newbie question. My code is under here:

Code:
<html>
<head>
<title>
<script language="JavaScript">
<!--
var usernum;
usernum = prompt("Write the 4 numbers ", "Write them here")
{
switch(usernum){
case "5050":
alert("310000");
break;
case "4957":
alert("567489);
break;
}
       }
//  -->
</script>
</head>
<body>

</body>
</html>
 
Last edited by a moderator:
There's no semicolon after your assignment to usernum. There seems to be an extra set of braces {} around the switch, but I think that's OK in JavaScript.

-Lee
 
There's no semicolon after your assignment to usernum. There seems to be an extra set of braces {} around the switch, but I think that's OK in JavaScript.

-Lee

I tried to change that, but it's still not working?
 
OK. Put your current code in [code][/code] tags. Then tell us what the error is.

-Lee

I'm not sure what you mean with [code][/code] and i can't tell you what the error is, because i can't find debugger for javascript for mac.
 
I'm not sure what you mean with [code][/code] and i can't tell you what the error is, because i can't find debugger for javascript for mac.

A moderator edited your first post to add code tags. You can press edit and see how these tags are used.

You are saying you don't know what the error is , but you said "it won't build it for me". What did you mean? How do you know there's a problem?

-Lee
 
What are you using to write your HTML file?

What browser are you using to open it and run the JavaScript?


Safari has a Develop menu that you can show by choosing:
Safari > Preferences > Advanced
then click the checkbox "Show Develop menu in menu bar

With Safari's Develop menu showing, choose Show Error Console.
Reload the web page and take note of any errors it tells you.

http://developer.apple.com/library/...afariDeveloperTools/SafariDeveloperTools.html

More Safari developer references:
http://developer.apple.com/library/safari/


In this fragment from your original code:
Code:
case "4957":
[COLOR="Red"]alert("567489);[/COLOR]
break;
The red-hilited line is missing a closing quote.


You might want to do some basic searching to find tools and websites for developing JavaScript in Safari.
Example search terms:
safari debug javascript
safari develop javascript
 
A moderator edited your first post to add code tags. You can press edit and see how these tags are used.

You are saying you don't know what the error is , but you said "it won't build it for me". What did you mean? How do you know there's a problem?

-Lee

I know there is an error, because i have been testing the script at this website: http://www.w3schools.com/js/tryit.asp?filename=tryjs_intro and when it works, it execute the script, but now it's not executing the script.
 
What are you using to write your HTML file?

What browser are you using to open it and run the JavaScript?


Safari has a Develop menu that you can show by choosing:
Safari > Preferences > Advanced
then click the checkbox "Show Develop menu in menu bar

With Safari's Develop menu showing, choose Show Error Console.
Reload the web page and take note of any errors it tells you.

http://developer.apple.com/library/...afariDeveloperTools/SafariDeveloperTools.html

More Safari developer references:
http://developer.apple.com/library/safari/


In this fragment from your original code:
Code:
case "4957":
[COLOR="Red"]alert("567489);[/COLOR]
break;
The red-hilited line is missing a closing quote.


You might want to do some basic searching to find tools and websites for developing JavaScript in Safari.
Example search terms:
safari debug javascript
safari develop javascript

Well, i tried to change that and it's still not working...
 
Well, i tried to change that and it's still not working...

Your basic formatting is fundamentally broken... Time to go back to Javascript 101. I hate to say it, but even with this, you won't get too much further if you don't reread the Javascript basics.

Code:
<html>
<head>
<title>
<script language="JavaScript">
<!--
var usernum = prompt("Write the 4 numbers ", "Write them here");
switch(usernum){
    case "5050":
        alert("310000");
        break;
    case "4957":
        alert("567489");
    break;
}
//  -->
</script>
</head>
<body>

</body>
</html>
 
Well, i tried to change that and it's still not working...

You also have this
Code:
[COLOR="Red"]<title>[/COLOR]
<script language="JavaScript">
There are two things wrong here:
1. You're not allowed to put a script inside a title element.
2. The <title> tag is missing a matching </title> tag.

The w3schools URL you linked to has none of the mistakes you've posted. It doesn't even have a switch statement. If you're going to modify what the tutorial provides that much, you should at least try getting farther through the tutorial. I've been through that tutorial, and I recommend it.

What I don't recommend is making lots of changes without testing each step. The "Edit and Click Me" button simply doesn't have good debugging or error-reporting capabilities. If you're serious about it, you'll look for some other tools, such as the ones I already linked to.


... I'm not sure if you commented out all your JavaScript on purpose or not, but that will obviously prevent execution.

Actually, it won't. The <!-- is a holdover from days when the <script> tag might not be recognized. It's an HTML comment, not a JavaScript comment.

Douglas Crockford's book "JavaScript: The Good Parts" is worth having. It's not for beginners, because it's not step-by-step, but experienced programmers will learn a lot from it.
 
Last edited:
You're still not posting the non-working code. I'm not sure if you commented out all your JavaScript on purpose or not, but that will obviously prevent execution.

-Lee

<!-- and --> is an HTML comment, not a Javascript comment. While not still strictly needed, it is leftover from the days when a developer had to hide Javascript from a browser that did not understand it. This is valid format.

Edit:

Ha, beat me

chown33 said:
Actually, it won't. The <!-- is a holdover from days when the <script> tag might not be recognized. It's an HTML comment, not a JavaScript comment.

Douglas Crockford's book "JavaScript: The Good Parts" is worth having. It's not for beginners, but experienced programmers will learn a lot from it.
 
It works with a few minor tweaks.

Code:
<html>
<head>
<title>Test</title>
<script language="JavaScript">
//I don't like to run code without calling it
//So I put it in a function check the body tag, it calls the function

function TestThis() {
var usernum;

usernum = prompt("Write the 4 numbers ", "Try 5050 here")

switch(usernum){
  case "5050":
	alert("310000");
	break;
  case "4957":
	alert("567489"); //open quote was here. 
	break;
  default: //I like to include a graceful way out. 
  document.getElementById("testText").innerHTML="You didn't enter a number I recognized, reload the page to try again.";
  }
} //end the function

</script>
</head>
<body onload="TestThis();">
<div id="testText">This is a test.</div> <!--I put this in a div, so I could change it based on a wrong answer above. -->
</body>
</html>
 
It works with a few minor tweaks.

Code:
<html>
<head>
<title>Test</title>
<script language="JavaScript">
//I don't like to run code without calling it
//So I put it in a function check the body tag, it calls the function

function TestThis() {
var usernum;

usernum = prompt("Write the 4 numbers ", "Try 5050 here")

switch(usernum){
  case "5050":
	alert("310000");
	break;
  case "4957":
	alert("567489"); //open quote was here. 
	break;
  default: //I like to include a graceful way out. 
  document.getElementById("testText").innerHTML="You didn't enter a number I recognized, reload the page to try again.";
  }
} //end the function

</script>
</head>
<body onload="TestThis();">
<div id="testText">This is a test.</div> <!--I put this in a div, so I could change it based on a wrong answer above. -->
</body>
</html>

I tried this and it works now. I have, however, run into another problem. I'm trying to run my javascript directly in my browser from the file on my computer, but all it's returning is the text of the javascript, not the order? I should probably mention, that i'm trying to do this on a mac and the files i'm running the code from is .html
 
Last edited:
I tried this and it works now. I have, however, run into another problem. I'm trying to run my javascript directly in my browser from the file on my computer, but all it's returning is the text of the javascript, not the order? I should probably mention, that i'm trying to do this on a mac and the files i'm running the code from is .html

Since you do not make any specific request to server, it doesn't matter if you run it from your computer or from a website (nor if you run it from Mac or PC). All the javascript is handled by the browser. So basically, if the script don't give the expected response, you still go a problem with the script.


What do you mean exactly by "not the order"? What are trying to accomplish with this script?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.