Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Simple loop question

1. ## 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

• 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

• 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>```

• 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.

• ## 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: 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/>

• *SIGH*. That's two times in three days. My mind seems to be disconnected from my fingers, lately. Thanks, Philip.

• 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)!

• 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.

• Originally Posted by Old Pedant
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...'

• Originally Posted by Old Pedant
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.

• Originally Posted by Philip M
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??

• ## Working version with room to grow, if needed

Originally Posted by Old Pedant
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/>
Originally Posted by jmrker
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>```

• LOL! I wonder if poor "reddychops" will ever ask a question here, again!!

Or will ever come back to see this thread.

• Originally Posted by Old Pedant
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 Last

#### Posting Permissions

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