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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple loop question

    I have the following code:
    Code:
      if (n==1 && g <=5)
        {
            sndPlayer1.URL ="Boy-a1.wav";
        }
        else if (n==1 && g >5)
            {
                sndPlayer1.URL ="Girl-a1.wav";
            }
        else if (n==2 && g <=5)
        {
            sndPlayer1.URL ="Boy-e1.wav";
        }
        else if (n==2 && g >5)
            {
                sndPlayer1.URL ="Girl-e1.wav";
            }
           else if (n==3 && g <=5)
        {
            sndPlayer1.URL ="Boy-i1.wav";
        }
        else if (n==3 && g >5)
            {
                sndPlayer1.URL ="Girl-i1.wav";
            }
           else if (n==4 && g <=5)
        {
            sndPlayer1.URL ="Boy-o1.wav";
        }
        else if (n==4 && g >5)
            {
                sndPlayer1.URL ="Girl-o1.wav";
            }
           else if (n==5 && g <=5)
        {
            sndPlayer1.URL ="Boy-u1.wav";
        }
        else if (n==5 && g >5)
            {
                sndPlayer1.URL ="Girl-u1.wav";
            }
          else if (n==6 && g <=5)
        {
            sndPlayer1.URL ="Boy-b1.wav";
        }
        else if (n==6 && g >5)
            {
                sndPlayer1.URL ="Girl-b1.wav";
            }
        else if (n==7 && g <=5)
        {
            sndPlayer1.URL ="Boy-b1.wav";
        }
        else if (n==7 && g >5)
            {
                sndPlayer1.URL ="Girl-b1.wav";
            }
        else if (n==8 && g <=5)
        {
            sndPlayer1.URL ="Boy-h1.wav";
        }
        else if (n==8 && g >5)
            {
                sndPlayer1.URL ="Girl-h1.wav";
            }
    
         else if (n==9 && g <=5)
        {
            sndPlayer1.URL ="Boy-t1.wav";
        }
        else if (n==9 && g >5)
            {
                sndPlayer1.URL ="Girl-t1.wav";
            }
    I was wondering what the best way to make a smaller simpler version of this code is? Would it be using a loop? If so can you help me with how to

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,253
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    You can condense it by 2/3 just by cutting out wasted space and newlines.

    Code:
    var song;
    if (n==1 && g <=5){song="Boy-a1.wav"}
    else if (n==1 && g >5) {song="Girl-a1.wav"}
    // and so on.....
    sndPlayer1.URL = song;
    He thought he saw an Elephant,
    That practised on a fife:
    He looked again, and found it was
    A letter from his wife.
    "At length I realise," he said,
    "The bitterness of Life!"
    - Lewis Carroll

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Code:
    <script type='text/javascript'>
    
    function getWav( n, g )
    {
     var table = [ n==1 && g <=5, "Boy-a1.wav", 
                   n==1 && g > 5, "Girl-a1.wav", 
                   n==2 && g <=5, "Boy-e1.wav",
                   n==2 && g > 5, "Girl-e1.wav" ],
                   
         theWav = null;
     
     for( var i = 0; i < table.length && !theWav; i += 2 )
      if( table[ i ] )
       theWav = table[ i + 1 ];
     
     return theWav;
    }
    
    
    alert( getWav( 1, 6 ) );
    
    alert( getWav( 2, 0 ) );
    
    </script>

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,206
    Thanks
    80
    Thanked 4,566 Times in 4,530 Posts
    Philip and Logic just like long solutions, I think.

    Code:
    if ( n < 1 || n > 9 )
    {
        alert( "Error: value of n is " + n + ", out of range";
    } else {
        var letters = "?aeioubbht";
        var name = ( g <= 5 ) ? "Boy-" : "Girl-";
        sndPlayer1.URL = name + letters.charAt(n) + "1.wav";
    
    }
    I assume you are aware that your values for n==6 and n==7 are the same.

    Can even be slightly more compact, of course:
    Code:
    if ( n < 1 || n > 9 )
    {
        alert( "Error: value of n is " + n + ", out of range";
    } else {
        sndPlayer1.URL = ( g <= 5 ? "Boy-" : "Girl-" ) + ("?aeioubbht").charAt(n) + "1.wav";
    }
    &&&&&&&&&&&&&&&&

    EDIT: As Philip pointed out, old fumble fingers here omitted the right paren on the end of the alert( ). Both times.
    Last edited by Old Pedant; 12-20-2010 at 08:46 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,133
    Thanks
    38
    Thanked 504 Times in 498 Posts

    Lightbulb Alternate attempt ...

    Alternate attempt:
    Code:
    var wavFiles = [
      ['',''],
      ['Boy-a1','Girl-a1'],
      ['Boy-e1','Girl-e1'],
      ['Boy-i1','Girl-i1'],
      ['Boy-o1','Girl-o1'],
      ['Boy-u1','Girl-i1'],
      ['Boy-b1','Girl-b1'],
      ['Boy-h1','Girl-h1'],
      ['Boy-t1','Girl-t1']	// Note: no comma after last entry
    ];
    function getWavFile(n,g) {	// could add checks for parameter validity
      n = Number(n);
      if (g <= 5) { g = 0; } else { g = 1; }
      return wavFiles[n][g]+'.wav';
    }
    Untested, but it should work.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,206
    Thanks
    80
    Thanked 4,566 Times in 4,530 Posts
    jmrker: You missed the fact that n==6 and n==7 are the same. "b" needs to be repeated.

    Noble effort, but way way too long. <grin/>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,206
    Thanks
    80
    Thanked 4,566 Times in 4,530 Posts
    *SIGH*. That's two times in three days. My mind seems to be disconnected from my fingers, lately. Thanks, Philip.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,253
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    alert( "Error: value of n is " + n + ", out of range" ) ;

    You code is very compact but very incomprehensible to a beginner (hard to debug/maintain)!

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,206
    Thanks
    80
    Thanked 4,566 Times in 4,530 Posts
    I agree the second version is pretty obscure. But the first version seems clear enough, to me.

    Maybe I shouldn't use the ternary operator, but other than that...

    Okay, without ternary:
    Code:
    if ( n < 1 || n > 9 )
    {
        alert( "Error: value of n is " + n + ", out of range" );
    } else {
        // separate out the code to choose boy/girl from the rest:
        var name;
        if ( g <= 5 ) { name = "Boy-" } else { name = "Girl-"; }
    
        // so the letters correspond directly to the value of n that was passed in
        // if n==1, then the letter is "a".  If n==9, the letter is "t".  and so on
        // n==0 is an error, taken care of above, so I put a "?" for letter zero.
        var letters = "?aeioubbht";
        var letter = letters[n]; // so choose the letter corresponding to n
    
        // and then getting the URL is easy:
        sndPlayer1.URL = name + letter + "1.wav";
    
    }
    There. Is that better? Reasonably clear, and still much more compact.

    I think it actually describe the selection process *better* than JMrker's array scheme, in the sense that it separates out the reason for picking a given letter from the reason for picking Boy vs. Girl.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,133
    Thanks
    38
    Thanked 504 Times in 498 Posts

    Arrow

    Quote Originally Posted by Old Pedant View Post
    jmrker: You missed the fact that n==6 and n==7 are the same. "b" needs to be repeated.

    Noble effort, but way way too long. <grin/>
    I got lost in the 3 page original post.
    As I said in my post: 'Untested...'

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,253
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Quote Originally Posted by Old Pedant View Post
    I agree the second version is pretty obscure. But the first version seems clear enough, to me.

    Maybe I shouldn't use the ternary operator, but other than that...
    You code is perfectly clear - to me, but perhaps not to the OP.
    My point is that if for some reason the OP decides he wants to play "The Star Spangled Banner" and "On Ilkley Moor Baht 'at" then it is a lot harder to modify the code.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,206
    Thanks
    80
    Thanked 4,566 Times in 4,530 Posts
    Quote Originally Posted by Philip M View Post
    My point is that if for some reason the OP decides he wants to play "The Star Spangled Banner" and "On Ilkley Moor Baht 'at" then it is a lot harder to modify the code.
    ROTFLMAO! Point taken. But...

    What about if he wants to play "Itsy Bitsy Spider" when g==7 and n==4?? Maybe we need to create a solution for all possible combinations of g and n??
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,133
    Thanks
    38
    Thanked 504 Times in 498 Posts

    Lightbulb Working version with room to grow, if needed

    Quote Originally Posted by Old Pedant View Post
    jmrker: You missed the fact that n==6 and n==7 are the same. "b" needs to be repeated.

    Noble effort, but way way too long. <grin/>
    Quote Originally Posted by jmrker View Post
    I got lost in the 3 page original post.
    As I said in my post: 'Untested...'
    Following works for 2 files per 'n' element, or could be expanded for variable # of entries (see Note
    Code:
    <html>
    <head>
    <title>Wave File Selection</title>
    <script type="text/javascript">
    // From: http://www.codingforums.com/showthread.php?p=1030196#post1030196
    
    var wavFiles = [
      ['Boy-a1','Girl-a1'],		// 0
      ['Boy-a1','Girl-a1'],		// 1
      ['Boy-e1','Girl-e1'],		// 2
      ['Boy-i1','Girl-i1'],		// 3
      ['Boy-o1','Girl-o1'],		// 4
      ['Boy-u1','Girl-i1'],		// 5
      ['Boy-b1','Girl-b1'],		// 6
      ['Boy-b1','Girl-b1'],		// 7
      ['Boy-h1','Girl-h1'],		// 8
      ['Boy-t1','Girl-t1']		// 9
    // Note: no comma after last entry
    ];
    
    /* Additional note:
       should be easy to add different file names to 'wavFiles' for full use of array. for example:
       ['Boy-a1','Boy-b1','Boy-c1','Boy-d1','Boy-e1','Boy-f1','Girl-a1','Girl-b1','Girl-c1','Girl-d1','Girl-e1','Girl-f1'];
       for [1] wavFiles element (etc.)
       then selection would be:
       alert(getWavFile(1,6])); 	// to choose wavFiles[1,6] or "Girl-a1'
    */
    
    function getWavFile(n,g) {	// could add checks for parameter validity
      n = Number(n);
      if (g <= 5) { g = 0; } else { g = 1; }	// only needed if only two file to select from
      return wavFiles[n][g]+'.wav';
    }
    function randomWavFile() {		// get random selection
      var n = Math.floor(Math.random() * wavFiles.length);
      var g = Math.floor(Math.random() * 10);
    
    /* Comment below is associated with additional note above:
       if more than 2 files per element, then:
         var g = Math.floor(Math.random() * wavFiles[n].length);
       and remove "if (g <= 5) ..." in getWavFunction if g is to be specified
    */
    
      return 'Chosen: '+n+' : '+g+'\n'+getWavFile(n,g);
    }
    </script>
    </head>
    <body>
    <button onclick="alert(randomWavFile())">Random Selection</button>
    <div id="showChoice"></div>
    </body>
    </html>

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,206
    Thanks
    80
    Thanked 4,566 Times in 4,530 Posts
    LOL! I wonder if poor "reddychops" will ever ask a question here, again!!

    Or will ever come back to see this thread.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #15
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,133
    Thanks
    38
    Thanked 504 Times in 498 Posts

    Arrow

    Quote Originally Posted by Old Pedant View Post
    LOL! I wonder if poor "reddychops" will ever ask a question here, again!!

    Or will ever come back to see this thread.
    Well, even if he/she doesn't, I've had a lot of fun watching it develop.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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