Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Oct 2007
    Posts
    73
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Switch and Integers

    Hi,

    I'm just starting with javascript and I can't figure out why this code isn't working. It always gives me the default.
    I know this is the problem, but why doesn't it work?
    case (aday <= weekdays.length):

    PHP Code:
    <script type="text/javascript">
        var 
    weekdays = new Array("monday","tuesday","wednesday","thursday","friday","saturday","sunday");
        var 
    getal prompt("Give a weekday number");
        var 
    aday parseInt(getal);

    switch(
    aday)
        {
            case (
    aday <= weekdays.length):
            
    document.write("<br /><span class=\"green\">" weekdays[aday-1] + "</span>");
            break;
            
            default:
            
    document.write("<br /><span class=\"red\">Only 7 days in a week!</span>");
        }
    </script> 
    Thank you.
    Last edited by friz; 09-07-2012 at 10:42 PM.

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Case uses strings or variables; you are providing a boolean (true/false). I don't think that will work.

    http://www.w3schools.com/js/js_switch.asp
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    New Coder
    Join Date
    Oct 2007
    Posts
    73
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Thanks for the quick reply.

    I really thought something simple as this would work
    So, switch checks only for specific values? And for eg booleans I should use if/else or do/while?

  • #4
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Yes, switch only (AFAIK) works with integers and strings. For a boolean, the if/else would probably be your best bet.

    Alternatively, you can assign several case statements to one action, so you could do:

    Code:
    switch(aday){
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
                       do stuff
      break;
    
      default:
                        do something else
      break;
    }
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • Users who have thanked WolfShade for this post:

    friz (09-07-2012)

  • #5
    New Coder
    Join Date
    Oct 2007
    Posts
    73
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Yeah, I saw that last example somewhere on the net while doing some searching on the topic. I used the weekdays just as an example. I don't see myself using switch that way for big arrays though

    I know about switch from working with php and I just wondered why a boolean didn't work. I thought I did something wrong.

    And now I see I misinterpreted this

    I changed my code to this:
    PHP Code:
    <script type="text/javascript">
        var 
    weekdays = new Array("monday","tuesday","wednesday","thursday","friday","saturday","sunday");
        var 
    getal prompt("Give a weekday number");
        var 
    aday parseInt(getal);

    switch(
    true)
        {
            case (
    aday <= weekdays.length):
            
    document.write("<br /><span class=\"green\">" weekdays[aday-1] + "</span>");
            break;
            
            default:
            
    document.write("<br /><span class=\"red\">Only 7 days in a week!</span>");
        }
    </script> 
    Looks like the parseInt isn't necessary too. This works as well:

    PHP Code:
    <script type="text/javascript">
        var 
    weekdays = new Array("monday","tuesday","wednesday","thursday","friday","saturday","sunday");
        var 
    getal prompt("Give a weekday number");

    switch(
    true)
        {
            case (
    getal <= weekdays.length):
            
    document.write("<br /><span class=\"groen\">" weekdays[getal-1] + "</span>");
            break;
            
            default:
            
    document.write("<br /><span class=\"rood\">Only 7 days in a week!</span>");
        }
    </script> 
    Last edited by friz; 09-07-2012 at 10:45 PM.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by friz View Post
    Looks like the parseInt isn't necessary too. This works as well:

    PHP Code:
    <script type="text/javascript">
        var 
    weekdays = new Array("monday","tuesday","wednesday","thursday","friday","saturday","sunday");
        var 
    getal prompt("Give a weekday number");

    switch(
    true)
        {
            case (
    getal <= weekdays.length):
            
    document.write("<br /><span class=\"groen\">" weekdays[getal-1] + "</span>");
            break;
            
            default:
            
    document.write("<br /><span class=\"rood\">Only 7 days in a week!</span>");
        }
    </script> 


    The parseInt() or other better string>number conversion is not necessary here, but as a matter of good coding practice should always be performed nevertheless. All inputs in Javascript are strings unless converted to numbers. Although Javascript freely type-converts values into a type suitable for (or required by) the context of their use, problems can still arise, especially with + (addition/concatenation).

    alert (typeof getal); // string

    By convention the days of the week in Javascript run from Sunday to Saturday, so Sunday is day 0, not day 7, and Saturday is Day 6.

    parseInt() is an inefficient way to convert a string to a number and is not really intended for that purpose. Use *1 or Number() instead. If you do insist on parseInt() you must declare the radix as otherwise a number starting with 0 is interpreted as octal.

    var aday = parseInt(getal,10);
    Last edited by Philip M; 09-08-2012 at 08:46 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New Coder
    Join Date
    Oct 2007
    Posts
    73
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Hi Philip,

    The parseInt() or other better string>number conversion is not necessary here, but as a matter of good coding practice should always be performed nevertheless.
    I did the typeof part, that's why I used the parseInt in the first example, but you're right, I forgot to copy the radix, which I had in my original code.

    I'm just testing things and I noticed it still worked without the parseInt, that's all.

    By convention the days of the week in Javascript run from Sunday to Saturday, so Sunday is day 0, not day 7, and Saturday is Day 6.
    As I've written in a previous post, my lack of imagination made me use the weekdays for testing. I can't see why the start day would matter in a simple array though. And where I live the weeks starts on Monday

    Thanks for the info on parseInt vs Number(). I thought it didn't matter, but I'll use Number() in the future


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •