...

View Full Version : Simple loop question



caishreddychops
12-20-2010, 06:30 PM
I have the following 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 :thumbsup:

Philip M
12-20-2010, 07:19 PM
You can condense it by 2/3 just by cutting out wasted space and newlines.


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

Logic Ali
12-20-2010, 07:22 PM
<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>

Old Pedant
12-20-2010, 08:20 PM
Philip and Logic just like long solutions, I think.



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:


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.

jmrker
12-20-2010, 08:22 PM
Alternate attempt:


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.
:)

Old Pedant
12-20-2010, 08:26 PM
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/>

Old Pedant
12-20-2010, 08:45 PM
*SIGH*. That's two times in three days. My mind seems to be disconnected from my fingers, lately. Thanks, Philip.

Philip M
12-20-2010, 08:49 PM
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)!

Old Pedant
12-20-2010, 09:06 PM
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:


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.

jmrker
12-20-2010, 09:07 PM
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. :o:eek:;):)
As I said in my post: 'Untested...' :thumbsup:

Philip M
12-20-2010, 09:32 PM
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.

Old Pedant
12-20-2010, 09:50 PM
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??

jmrker
12-20-2010, 11:03 PM
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. :o:eek:;):)
As I said in my post: 'Untested...' :thumbsup:

Following works for 2 files per 'n' element, or could be expanded for variable # of entries (see Note:)


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

Old Pedant
12-20-2010, 11:11 PM
LOL! I wonder if poor "reddychops" will ever ask a question here, again!!

Or will ever come back to see this thread.

jmrker
12-21-2010, 12:08 AM
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. ;)

Philip M
12-21-2010, 08:44 AM
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??

Well, that is my point. The simple code in Post#3 can easily be modified in any way you want. Yours cannot. :p

As I have said before, I much prefer clarity, flexibility and maintainability to simply saving a few lines of code, which these days makes not a whit of difference to the speed of execution. :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum