Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Random number combined with switch does not work

  1. #1
    Join Date
    Apr 2006
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Random number combined with switch does not work

    Code:
    document.write("<img src='http://www.a2h.8m6.net/images/logo.php?msg=");
    // Generates a random number between 1 and 10
    var randomnumber=Math.floor(Math.random()*6)
    swExmpl(randomnumber);
    function swExmpl(ex)
    {
    switch (ex)
    {
        case 1: { document.write("now with 10%25 more potatoes!"; break); break }
        case 2: { document.write('anger2headshot is not a crack dealer.'); break }
        case 3: { document.write("here is your free complementary wastage of your internet bandwith..."); break }
        case 4: { document.write("CHUCK NORRIS IS BEHIND YOU!!!"); break }
        case 5: { document.write("free lying parrot not provided."); break }
    }
    }
    document.write("'><br>");
    This code does not work, can anyone tell me why?

  2. #2
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well I think should tell you this but you probably already know, here goes -

    Use if and else if and else instead of switch, because -

    1) Its less complicated
    2) For all I know switch can not take two or more conditions at once
    3) Its more practical...

    Inform me of your decision...

  3. #3
    Join Date
    Apr 2006
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ah, thanks.

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    It's also much more efficient in using arrays then if statements as well (though your point is taken, switch is possibly the worst):
    Code:
    var msg = ["message","message","message","message","message","message"];
    for (var i=0;i<msg.length) document.write(msg[Math.floor(Math.random()*6)]);
    - Mike

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You also are better off concatenating the HTML and then writing it out all at once, ex;

    Code:
    var HTMLstr='';
    HTMLstr+='<img src="whatever.php?msg=';
    HTMLstr+='Some random message';
    HTMLstr+='"><br>';
    document.write(HTMLstr);
    Otherwise, some browsers may automatically close the tag before you intend it to be closed.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    All true, except what pcbrainbuster said, but the reason your original code didn't work are these:
    Code:
        case 1: { document.write("now with 10&#37;25 more potatoes!"; break); break [color=red]}
    Switch statements use a series of labels, not code blocks (one more reason they should be outlawed ), and that break has no business being inside a function's brackets. Also, you've failed to account for case 0, and non-Javascript browsers.
    Code:
    <img src="http://www.a2h.8m6.net/images/logo.php?msg=some%20default%20message" alt="Banner" id="banner">
    <script type="text/javascript" src="http://www.twey.co.uk/pythonic.js"></script>
    <script type="text/javascript">
      Twey.Pythonic.LOAD("Array.randomChoice");
      document.images['banner'].src = "http://www.a2h.8m6.net/images/logo.php?msg=" +
        encodeURIComponent([
          "now with 10% more potatoes!",
          "anger2headshot is not a crack dealer.",
          "here is your free complementary wastage of your internet bandwidth...",
          "CHUCK NORRIS IS BEHIND YOU!!!",
          "free lying parrot not provided."
        ].randomChoice());
    </script>
    Untested.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Got an error, and it's saying that Twey.Pythonic isn't an object, or has no properties.
    But this will work:
    Code:
    <script type="text/javascript">
      Array.prototype.randomChoice = function() {
        return this[Math.floor(Math.random() * this.length)];
        };
      document.images['banner'].src = "http://www.a2h.8m6.net/images/logo.php?msg=" +
        encodeURIComponent([
          "now with 10&#37; more potatoes!",
          "anger2headshot is not a crack dealer.",
          "here is your free complementary wastage of your internet bandwidth...",
          "CHUCK NORRIS IS BEHIND YOU!!!",
          "free lying parrot not provided."
        ].randomChoice());
    </script>
    - Mike

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Whoops, name difference left over from debugging. Fixed now, but yeah, that'll work just as well.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    BTW, that's some complex script you have going there. How do you call functions with string names?
    example:
    Code:
        'reduce' : function(f) {
          if(this.length === 0)
            return null;
          for(var i = 1, c = this[0]; i < this.length; ++i)
            c = f(c, this[i]);
          return c;
        },
    Twey.pythonic["reduce"](); ?
    Never used anything like that before.
    - Mike

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Twey.pythonic["reduce"](); ?
    Twey.Pythonic.Array.reduce() would call the function. It still wouldn't work, though, since it's meant to be called as a property of an array, which is what the LOAD() function is for. The end result is a fairly neat way (I think) of avoiding cluttering up native prototypes, especially since that file contains most of the functions I find myself writing from scratch for scripts, so in most cases only one or two will actually be used.
    Last edited by Twey; 04-17-2007 at 06:02 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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