|
From: Mark on 9 Jan 2006 04:39 Hi - how can I get a switch statement to look for a range of values? I have: function payCalc(field, field2) switch(field.value) { case 0-50: field2.value="lower band"; case 51-99: field2.value="mid band"; case 100-9999: field2.value="high band"; } But this doesn't seem to work. Thanks for any help, Mark *** Sent via Developersdex http://www.developersdex.com ***
From: Evertjan. on 9 Jan 2006 05:14 Mark wrote on 09 jan 2006 in comp.lang.javascript: > Hi - how can I get a switch statement to look for a range of values? > > I have: > > function payCalc(field, field2) > switch(field.value) > { > case 0-50: field2.value="lower band"; > case 51-99: field2.value="mid band"; > case 100-9999: field2.value="high band"; > } > > But this doesn't seem to work. Switch is only handy for the godly that have read the specs. For us mere mortals, to lazy to read them, use: field2.value = (field.value<0) ? "out-of-range" : (field.value<51) ? "lower band" : (field.value<100) ? "mid band" : (field.value<10000)? "high band" : "out-of-range" ; -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
From: VK on 9 Jan 2006 07:54 Mark wrote: > Hi - how can I get a switch statement to look for a range of values? "switch" statement doesn't directly support ranges (like in VBS / VBA). But you can work around that by comparing the case with true: var num = 8000; switch(true) { case num>=0&&num<=51: alert(1); break; case num<=99: alert(2); break; case num<=9999: alert(3); break; default: alert('Out of range'); } P.S. Unless you want to use (as suggested) more wide spread if-else if - else block. P.P.S. Also remember a very annoying cross-language bug in switch construct: it executes all underlaying branches unless you stop it with "break" statement.
From: Julian Turner on 9 Jan 2006 08:45 VK wrote: [snip] > P.P.S. Also remember a very annoying cross-language bug in switch > construct: it executes all underlaying branches unless you stop it with > "break" statement. [snip] Hello VK Just interested in why you consider this to be a "bug"? If I understand para 12.11 of the ECMA spec correctly (<URL:http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf>, the production for a CaseClause or DefaultClause does not require the "break" Statement to be included. Further, leaving out the break statement can IMHO be useful sometimes (if for instance you want a given Statement to be executed for more than one case). So perhaps rather than "bug", do you mean: "source of potential errors for the unwary"? Regards Julian Turner
From: VK on 9 Jan 2006 09:12
Julian Turner wrote: > VK wrote: > > [snip] > > P.P.S. Also remember a very annoying cross-language bug in switch > > construct: it executes all underlaying branches unless you stop it with > > "break" statement. > [snip] > > Hello VK > > Just interested in why you consider this to be a "bug"? As I escaped the classical Comp.Sci. education I dare to call bug on bug and sh** on sh** if I feel so :-)) In the particular I do not consider a nonsense to magically become a lore just of being put in written (whoever wasted inks for it). This has nothing to do with ECMA though - it's all the same: up to the most pre-historic languages. Why do I think it's a bug? Because it goes against the common programming logic - and this is why people keep forgetting about break's. It's like having to put break after each function body so execution wouldn't jump on underlaying function: function foo() { // ... } break; function bar() { // ... } - and w/o break after foo() we would immediately execute bar(). A lot of sense? None. The same amount in case X : ...; break; // IMHO A perfect case of reversed logic for me. That would have some sense maybe to have a flag to "continue" execution of underlaying branches - but not a current annoyance twist. IMHO IMHO IMHO |