Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-20-2010, 05:30 PM   PM User | #1
caishreddychops
New to the CF scene

 
Join Date: Dec 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
caishreddychops is an unknown quantity at this point
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
caishreddychops is offline   Reply With Quote
Old 12-20-2010, 06:19 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
Philip M is offline   Reply With Quote
Old 12-20-2010, 06:22 PM   PM User | #3
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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>
Logic Ali is offline   Reply With Quote
Old 12-20-2010, 07:20 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
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.

Last edited by Old Pedant; 12-20-2010 at 07:46 PM..
Old Pedant is offline   Reply With Quote
Old 12-20-2010, 07:22 PM   PM User | #5
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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.
jmrker is offline   Reply With Quote
Old 12-20-2010, 07:26 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-20-2010, 07:45 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
*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.
Old Pedant is offline   Reply With Quote
Old 12-20-2010, 07:49 PM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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)!
Philip M is offline   Reply With Quote
Old 12-20-2010, 08:06 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-20-2010, 08:07 PM   PM User | #10
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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...'
jmrker is offline   Reply With Quote
Old 12-20-2010, 08:32 PM   PM User | #11
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 12-20-2010, 08:50 PM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-20-2010, 10:03 PM   PM User | #13
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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>
jmrker is offline   Reply With Quote
Old 12-20-2010, 10:11 PM   PM User | #14
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-20-2010, 11:08 PM   PM User | #15
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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.
jmrker is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:50 AM.


Advertisement
Log in to turn off these ads.