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
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?
^_^
what defines default in your script? are you sure you don't need to use "case else" ?
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.
http://www.w3schools.com/JS/js_switch.asp
It should be default: blah blah blah
There is no "case default" in JavaScript syntax
Originally Posted by
sohguanh
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
Forum Rules
Bookmarks