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

Thread: Can I use for loops inside switch{} ??

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can I use for loops inside switch{} ??

    I think that my title says it all. Well, if you don't get what I mean here's what I mean. Can I use for loops in switch statements like this:

    Code:
    switch(somevariable){
    for(var i=0;i<somevalue;i++;j=10;j<someothervalue;j++){
    case i:
    alert(somearray[j]);
    break;
    }
    }
    Thank you for your precious time reading my post.

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

    Default

    Not like that, no. The question arises, of course, of why you'd want to. The example you've given can be accomplished with:
    Code:
    for(var i = 0; i < somevalue; ++i)
      if(somearray[i + 10])
        alert(somearray[i + 10]);
    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!

  3. #3
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Twey, but how do I use that inside a switch statement??

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

    Default

    Why would you want to? The loop I provided duplicates the functionality of what you're trying to do with the pseudo-Javascript above. If you tell us what you're trying to do, we can think of a better way to do it.
    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!

  5. #5
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I am trying to make a 12 hour clock(maybe there are other easier ways but I want it this way).

    Code:
    d = new Date();
    hours = d.getHours();
    minutes = d.getMinutes();
    seconds = d.getSeconds();
    switch(hours){
    for(var i=12;i<24;i++;var j=1;j<12;j++){
    case i:
    hours = j;
    break;
    }
    }
    document.getElementById("somediv").innerHTML = hours + minutes + seconds;
    setTimeout("somefunc()",1000);

  6. #6
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Code:
    window.onload = startClock;
    function startClock(){
    setClock();
    setInterval("setClock()",1000);
    }
    function setClock(){
    var d = new Date();;
    var hours = check(d.getHours());
    var minutes = check(d.getMinutes());
    var seconds = check(d.getSeconds());
    document.getElementById('clock').innerHTML = hours+':'+minutes+':'+seconds
    }
    function check(num){
    if(num<10){
    num = '0'+num;
    }
    return num;
    }
    Quick javascript clock for you

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

    Default

    Code:
    window.onload = function() {
      var a = function(n) {
        return (n.toString().length < 2 ? "0" + n : n);
      };
      var f = function() {
        var d = new Date();
        window.document.getElementById("clock").firstChild.nodeValue = a(d.getHours()) + ':' + a(d.getMinutes()) + ':' + a(d.getSeconds());
      };
      f();
      window.setInterval(f, 1000);
    };
    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!

  8. #8
    Join Date
    Sep 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    can someone read http://www.dynamicdrive.com/forums/s...ad.php?t=13413 and help me out please... its for school

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

    Default

    1. We don't do homework.
    2. Why on earth did you decide to post in this thread?
    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!

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

    Default

    Quote Originally Posted by blm126
    Quick javascript clock for you
    Shachi wanted a *12 hour clock. Not 24
    - Mike

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
  •