switch statement is driving me crazy

Discussion in 'Javascript' started by mark4asp, Dec 14, 2007.

  1. mark4asp

    mark4asp Guest

    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;

    }
     
    mark4asp, Dec 14, 2007
    #1
    1. Advertising

  2. mark4asp

    Stevo Guest

    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.
     
    Stevo, Dec 14, 2007
    #2
    1. Advertising

  3. mark4asp

    VK Guest

    > 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;
    }
     
    VK, Dec 14, 2007
    #3
  4. mark4asp

    Lee Guest

    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.


    --
     
    Lee, Dec 14, 2007
    #4
  5. mark4asp

    Doug Gunnoe Guest

    On Dec 14, 11:22 am, "mark4asp" <> wrote:

    > 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?
     
    Doug Gunnoe, Dec 14, 2007
    #5
  6. mark4asp

    VK Guest

    On Dec 14, 11:53 pm, Randy Webb <> wrote:
    > 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.
     
    VK, Dec 14, 2007
    #6
  7. mark4asp

    mark4asp Guest

    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 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.
     
    mark4asp, Dec 17, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Gower

    ItemTemplate error driving me crazy

    William Gower, Apr 23, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    321
    William Gower
    Apr 23, 2004
  2. =?Utf-8?B?VGltOjouLg==?=

    Insert and Variables driving me crazy! Newbie...

    =?Utf-8?B?VGltOjouLg==?=, Dec 20, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    339
    =?Utf-8?B?VGltOjouLg==?=
    Dec 21, 2004
  3. Shapper
    Replies:
    0
    Views:
    336
    Shapper
    Apr 29, 2005
  4. Joe Blanchard via .NET 247
    Replies:
    1
    Views:
    764
    Eliyahu Goldin
    May 15, 2005
  5. Replies:
    3
    Views:
    432
    Kevin Spencer
    Jul 1, 2005
Loading...

Share This Page