www.webdeveloper.com
Results 1 to 4 of 4

Thread: turning a 'switch' statement into a 'for' loop?

  1. #1
    Join Date
    Nov 2003
    Location
    SA
    Posts
    261

    turning a 'switch' statement into a 'for' loop?

    Hi friends

    for some reason I cannot figure out this stupid little thing, maybe I must go sleep.

    I'm trying to use a for loop instead of a switch statement. The switch looks like this:
    PHP Code:
    switch(Number(abc))
    {
        case 
    1:
            
    playChance[1] = 1;
            
    playChance[2] = 2;
            
    playChance[3] = 3;
            break;
        case 
    2:
            
    playChance[1] = 2;
            
    playChance[2] = 1;
            
    playChance[3] = 3;
            break;
        case 
    3:
            
    playChance[1] = 3;
            
    playChance[2] = 1;
            
    playChance[3] = 2;
            break;

    and the for loop to replace the switch is not working, here's my frustration:
    PHP Code:
    playChance[1] = Number(abc);

    for(var 
    2<= 3i++)
    {
        for(var 
    13j++)
        {
            if(
    playChance[1] != j)
                
    playChance[i] = j;
        }

    I don't know what I'm doing anymore, can someone please help?

    Thanks a ton
    The best way to predict the future, is to invent it!

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    The switch is very readable. Abandon the incomprehensible other method or use the permutations !

  3. #3
    Join Date
    Nov 2003
    Location
    SA
    Posts
    261

    Thumbs up

    No idea what you mean Julien...

    anyway... here's the solution:
    PHP Code:
    var tcount 2;
    playChance[1] = Number(abc);
    for(var 
    1<= wheelXspliti++)
    {
        if(
    != playChance[1])
        {
            
    playChance[tcount] = i;
            
    tcount++;
        }

    the output result looks like this:
    // abc = 1
    playChance[1] = 1;
    playChance[2] = 2;
    playChance[3] = 3;

    // abc = 2
    playChance[1] = 2;
    playChance[2] = 1;
    playChance[3] = 3;

    // abc = 3
    playChance[1] = 3;
    playChance[2] = 1;
    playChance[3] = 2;
    Last edited by slyfox; 11-29-2012 at 12:29 PM. Reason: [SOLVED]
    The best way to predict the future, is to invent it!

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    You are probably right...

    Good luck to read this new code two months later !

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles