switch statement is driving me crazy

M

mark4asp

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;

}
 
S

Stevo

mark4asp said:
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.
 
V

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;
}
 
L

Lee

VK said:
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.


--
 
D

Doug Gunnoe

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?
 
V

VK

The OP's code has
the return statements (check it again).

Indeed. I should not answer by post titles: "switch statement is
driving me crazy" - so again another no break; victim :) :-|

Actually OP's post works just fine. He simply didn't use placeholders
for missing function arguments so called showReport something like
showReport('MANDATE'); instead of showReport(null,'MANDATE'). In any
case it was not showReport(5843,'MANDATE'); as stated - otherwise the
script would work.
 
M

mark4asp

mark4asp said:
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;

}

Thanks for all who answered this query. It does work now. I wasn't able
to figure out the error. Perhaps I was calling it wrongly, with the
wrong paramerters or paramerters in wrong order or missing paramerters
as VK suggested.

The funny thing was that I was debugging in Firebug and the fuction
seems to have got the correct arguments from the parameters. I guess
that stopped me looking further at the calling code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top