By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,616 Members | 1,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,616 IT Pros & Developers. It's quick & easy.

switch statement is driving me crazy

P: n/a


Every time the function below is called I get the alert. So I put a
deliberate error in there and I check the value of
(reportType=='MANDATE') in Firebug, which is found to be true. But
still the alert comes up. Why?
I checked the following watch expressions at the blah blah point.

id = 5843
reportType = "MANDATE"
(reportType=='MANDATE') = true

It is being called like so:

showReport(5843,'MANDATE');

So what is going on here? Please let me in on the secret of writing a
switch statement.
function showReport(id, reportType, bHighlightRow)
{
var url;
switch (reportType) {
case 'INVESTOR':
url = 'getFundProfile.aspx?Ref='
break
case 'SUMMARY':
url = 'getprofile.aspx?Summary='
break
case 'MANDATE':
url = 'getprofile.aspx?Ref='
break
default:
alert('Error - javascript:showReport() called with wrong args')
}

blah blah;

}
Dec 14 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
mark4asp wrote:
Every time the function below is called I get the alert. So I put a
deliberate error in there and I check the value of
(reportType=='MANDATE') in Firebug, which is found to be true. But
still the alert comes up. Why?
I just tried this and it alerts Mandate on IE and FF2.

<html><body>
<script language="javascript">
function showReport(id, reportType, bHighlightRow)
{
var url;
switch (reportType) {
case 'INVESTOR':
alert("Investor")
break
case 'SUMMARY':
alert("Summary")
break
case 'MANDATE':
alert("Mandate")
break
default:
alert('default')
}
}
showReport(5843,'MANDATE');
</script></body></html>

Is your code being compressed/minified and the lack of semi-colons is
catching you out? Although I would expect Firefox to be very verbose
about it in the Error Console if that were the case.
Dec 14 '07 #2

P: n/a
VK
switch statement is driving me crazy

switch statement implements fall-through branching as opposed to the
exclusive branching implemented by if-else if-else statement. That
means that switch executes the branch where the condition is true and
all branches below that branch without further condition check. If you
want exclusive branching then use the right statement for that: if-
else if-else. If switch statement is too appealing to you, then add
break; statement at the end of each branch:

switch (condition) {
case 1 : statements;
break;
case 2 : statements;
break;
case 3 : statements;
break;
default: statements;
}
Dec 14 '07 #3

P: n/a
Lee
VK said:
>
>switch statement is driving me crazy

switch statement implements fall-through branching as opposed to the
exclusive branching implemented by if-else if-else statement. That
means that switch executes the branch where the condition is true and
all branches below that branch without further condition check. If you
want exclusive branching then use the right statement for that: if-
else if-else.
Actually, the switch statement is the correct choice when you
are comparing a variable against a number of literal values.
--

Dec 14 '07 #4

P: n/a
On Dec 14, 11:22 am, "mark4asp" <mark4...@gmail.comwrote:
id = 5843
reportType = "MANDATE"
(reportType=='MANDATE') = true

It is being called like so:

showReport(5843,'MANDATE');

So what is going on here? Please let me in on the secret of writing a
switch statement.

function showReport(id, reportType, bHighlightRow)
{
var url;
switch (reportType) {
case 'INVESTOR':
url = 'getFundProfile.aspx?Ref='
break
case 'SUMMARY':
url = 'getprofile.aspx?Summary='
break
case 'MANDATE':
url = 'getprofile.aspx?Ref='
break
default:
alert('Error - javascript:showReport() called with wrong args')
}

blah blah;

}- Hide quoted text -

- Show quoted text -
It works for me also. Can you post the rest of the script or a link?

Dec 14 '07 #5

P: n/a
VK said the following on 12/14/2007 12:39 PM:
>switch statement is driving me crazy

switch statement implements fall-through branching as opposed to the
exclusive branching implemented by if-else if-else statement. That
means that switch executes the branch where the condition is true and
all branches below that branch without further condition check.
Not entirely true. It will execute through branches until it encounters
either the end of the switch, or, a return statement. The OP's code has
the return statements (check it again).

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 14 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.