www.webdeveloper.com
Results 1 to 6 of 6

Thread: Syntax error in switch / case

  1. #1
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    585

    Syntax error in switch / case

    I am pulling my hair out over this problem on the default in the case/switch statement:
    Code:
    <html><head><title>testing</title>
    <script language="javascript" type="text/javascript">
    var errText = "";
    function findKey(token) {
    var result = new Array();
    var keyTab = [ ["Ab",8],["A ",3],["Bb",10],["B ",5],["Cb",5],["C ",0],["C#",7],["Db",7],["D ",2],
                   ["Eb",9],["E ",4],["F ",11],["F#",6],["Gb",6],["G ",1] ];
    var key = "", i, ikey, iadj=0, dosharp;
    var Sharpline = "F#,C#,G#,D#,A#,E#,B#"; var Flatline = "Bb,Eb,Ab,Db,Gb,Cb,Fb";
    
      result= token.match(/([A-G][#b]?) *([A-Za-z]{0,3})/);
      if (result.length > 1) {result[2] =  result[2].toLowerCase();}
    	if (result != null) {  //start of temp code
    	  alert("result length: " + result.length);
          for (i = 0; i < result.length; i++)
            alert(i + " ->" + result[i] + "<-");
          alert("lastmatch " + RegExp.lastMatch + " Index= " + result.index + " length key= " + result[0].length); }	// end of temp code  
       if (result[0].length == 1 && result[0] == "C")
         {return "C";}
       key = result[1];	 
       if (key.length == 1)
         {key += " ";}  //Trailing space for search
       for (i = 0; i < keyTab.length; i++) {
         if (key == keyTab[i][0]) {
    	   ikey = keyTab[i][1]; break; } }
       alert("ikey = " + ikey);
       if (ikey == keyTab.length) {
          errText+= "Did not recognize key: " + key + "\n";
          return "C"; }
       
       if ((result[2].length == 1) && (result[2] == "m")) {iadj = 9;} else {
       if (result[2].length == 3) {
         switch (result[2]) {
    	   case "min" : {iadj = 9; break;}
    	   case "mix" : {iadj = 11; break;}
    	   case "dor" : {iadj = 10; break;}
    	   case "phr" : {iadj = 8; break;}
    	   case "lyd" : {iadj = 1; break;}
    	   case "loc" : {iadj = 5; break;}
    	   case "maj" : {iadj = 0; break;}
    	   case default: {iadj = 0; errText += "Did not recognize mode: " + result[2] + "\n"; break;}  //Help!!!
    	 } } }
       ikey += iadj;
       ikey = ikey%12;
       if (ikey == 0) {
         return "C"; }
       if (ikey > 0 && < 5) {dosharp = 1;} else
       if (ikey > 7 && < 12) {dosharp = 0}; else
       if (iadj == 0 && key.indexOf("b")) {dosharp = 0;} else
       if (question("Use Flat Key?") == true) {dosharp = 0;} else {dosharp = 1;};
       if (dosharp == 1) {
         var ilen = ikey * 3 - 1;
    	 return Sharpline.substr(0,ilen); } 
        ilen = 51.5 - 4.5 * ikey;
        return Flatline.substr(0,ilen)	
       for (var i = 0; i < Sharps.length; i++) {
         if (key == Sharps[i].substr(0,2)) {
    	   return Sharpline.substr(0,Sharps[i].substr(2,2)); }}
       for (i = 0; i < Flats.length; i++) {
         if (key == Flats[i].substr(0,2)) {
    	   return Flatline.substr(0,Flats[i].substr(2,2)); }}
      }
    
    </script>
    </head>
    <body><script langauge="javascript" type="text/javascript">
    alert(findKey("B Min"));
    if (errText.length) alert(errText);
    </script>
    </body></html>
    I have used the switch statement in other parts of the script convert.htm , but don't have a clue on this.

    TIA

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Remove the curly brackets from your case statements.

    Code:
    // Change from this
        case "1": { action1;  break; }
        case "2": { action2;  break; }
    // .. to this
        case "1": 
            action1; 
        break;
        case "2": 
            action2; 
        break;
    What error message are you getting from it?

    ^_^

  3. #3
    Join Date
    Apr 2010
    Location
    Salem,Ma
    Posts
    623
    what defines default in your script? are you sure you don't need to use "case else" ?

  4. #4
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    585
    Using Firefox, I got "syntax error" and an error was pointing to the word "default". Just tried Dan's suggestion and it pointed to "else". If a mode was on that list it is acceptable, otherwise I ignore it and tell the user about it later.

    Taking out the "{}" didn't help. Thanks for looking at it.

  5. #5
    Join Date
    Mar 2010
    Location
    Singapore
    Posts
    367
    http://www.w3schools.com/JS/js_switch.asp

    It should be default: blah blah blah

    There is no "case default" in JavaScript syntax

  6. #6
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    585

    resolved

    Quote Originally Posted by sohguanh View Post
    http://www.w3schools.com/JS/js_switch.asp

    It should be default: blah blah blah

    There is no "case default" in JavaScript syntax
    Many thanks! That did it.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.17023 seconds
  • Memory Usage 2,897KB
  • Queries Executed 13 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (6)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates