PDA

View Full Version : found this code, dont understand it tho



darco9x2
03-27-2005, 03:27 AM
i found a code for background sound, using random sounds. but i dont understand.
ill post the part i dont understand.



var sound1="xxxxxx"
var sound2="xxxxxx"
var sound3="xxxxxx"
var sound4="xxxxxx"

var x=Math.random()*10
if (x<=3) x=sound1
else if (x<=6) x=sound2
else
x=sound3
if (navigator.appName=="Microsoft Internet Explorer")
document.write('<bgsound src='+'"'+x+'"'+' volume="lound" loop="infinite">')
else
document.write('<embed src='+'"'+x+'"'+'hidden="true" border="0" width="20"

height="20" autostart="true" volume="loud" loop="true">')

i dont understand how the random part works because it says what to do if x = higer or lower than a certain number. any help would be greatly appreciated.

jscheuer1
03-27-2005, 04:00 AM
Well, does it work? If so, does it do what you want? Those are the important questions. Now, for a translation:

var x=Math.random()*10 // x=a math object chosen at random times 10
if (x<=3) x=sound1 // if x is less than or equal to 3 we will use sound1
else if (x<=6) x=sound2 // if x is greater than 3 but less than or equal to 6 we will use sound2
else
x=sound3 // if x is greater than 6 we will use sound3

Note: Math.random is always greater than or equal to 0 and less than 1.

mwinter
03-27-2005, 12:41 PM
i found a code for background sound, using random sounds. but i dont understand.The code is badly written. A better approach would be:


var sound = [
'...',
'...',
'...',
'...'
],
i = Math.floor((Math.random() % 1) * sound.length);I think the code is self-explanatory: an array of URLs is created and assigned to the sound variable. Next, a random number in the range [0,1) is created and multiplied by the number of elements in the array. The resulting number, in the range [0,n), is then truncated to an integer; [0,n-1]. This final value will then index an element in the array: sound[i]. This approach is extensible as you could have as many, or as few, URLs as you like in the sound array without making changes to the code.

I can't say I'm impressed by the browser detection, but as I don't know the current support available, I can't suggest an alternative. You could probably use the object element as a catch-all, though it would only be supported by more modern user agents.

Mike

darco9x2
03-27-2005, 10:27 PM
[QUOTE=jscheuer1]
if (x<=3) x=sound1 // if x is less than or equal to 3 we will use sound1
QUOTE]

thats what i dont get, it works, but it also uses all sounds, even tho i only used 4, so the code just dosent make sense to me. yes, it works, but id like to get an understanding of why it works.

mwinter
03-28-2005, 12:38 PM
if (x<=3) x=sound1 // if x is less than or equal to 3 we will use sound1


thats what i dont get, it works, but it also uses all sounds, even tho i only used 4, so the code just dosent make sense to me. yes, it works, but id like to get an understanding of why it works.I'm finding it a little difficult to help as I don't understand the source of your confusion. Please explain in more detail what you find confusing. If it's because you expect the script to do something other than what it apparently does, tell us what you think it should do and why.

With the original code you posted, there was an if..else statement with three branches (reformatted):


if(x <= 3) {x = sound1;}
else if(x <= 6) {x = sound2;}
else {x = sound3;}The variable, x is a randomly generated number in the range [0,10). In other words, a number greater than or equal to zero, but less than ten.

The if..else statements define a series of three partitions (unequal ones, I might add). If the number is in the range [0,3] (greater than or equal to zero, but less than or equal to three), the first sound is used. If not, the next partition is evaluated: if the number is in the range (3,6] (greater than three, but less than or equal to six), the second sound is used. If the number is greater than six, the third sound is used. Notice that the fourth sound, defined by sound4 would never be used. That's really all there is to it.

Frankly, whoever wrote that originally is clueless. Not using an array, the inability to create fair selection, the browser detection, and the needless separate concatenation of the double quotes.

Mike