Javascript Switch limit?

Discussion in 'Web Design and Development' started by danewithoutwax, Jan 9, 2011.

  1. danewithoutwax, Jan 9, 2011
    Last edited by a moderator: Jan 9, 2011

    danewithoutwax macrumors newbie

    Joined:
    Aug 19, 2010
    #1
    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>
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. danewithoutwax thread starter macrumors newbie

    Joined:
    Aug 19, 2010
    #3
    I tried to change that, but it's still not working?
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    OK. Put your current code in [code][/code] tags. Then tell us what the error is.

    -Lee
     
  5. danewithoutwax thread starter macrumors newbie

    Joined:
    Aug 19, 2010
    #5
    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.
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    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
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Brobdingnag
    #7
    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
     
  8. danewithoutwax thread starter macrumors newbie

    Joined:
    Aug 19, 2010
    #8
    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.
     
  9. danewithoutwax thread starter macrumors newbie

    Joined:
    Aug 19, 2010
    #9
    Well, i tried to change that and it's still not working...
     
  10. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #10
    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>
    
     
  11. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #11
    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
     
  12. chown33, Jan 9, 2011
    Last edited: Jan 9, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Brobdingnag
    #12
    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.


    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.
     
  13. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #13
    <!-- 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

     
  14. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #14
    Fair enough. Got me. I guess this is why I don't hang out on the web programming forum.

    -Lee
     
  15. Zazoh macrumors 6502a

    Zazoh

    Joined:
    Jan 4, 2009
    Location:
    San Antonio, Texas
    #15
    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>
    
     
  16. danewithoutwax, Jan 10, 2011
    Last edited: Jan 10, 2011

    danewithoutwax thread starter macrumors newbie

    Joined:
    Aug 19, 2010
    #16
    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
     
  17. morespce54 macrumors 65816

    morespce54

    Joined:
    Apr 30, 2004
    Location:
    Around the World
    #17
    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?
     

Share This Page