/    Sign up×
Community /Pin to ProfileBookmark

Using random generator for two functions

Hi all –

I’m trying to write a script that generates a random piece of text xyz (I have that part done) that is used by two different functions: one that plays a sound file named xyz and another that reloads the js page with the addition of a line of text xyz. No matter what I do, I can’t get the two functions to both use the randomly generated text. In fact, all I’ve managed to do so far is get the text to pop up as an alert – I don’t know how to get it to reload the page with xyz added to it. Any ideas?

Thanks!

to post a comment
JavaScript

19 Comments(s)

Copy linkTweet thisAlerts:
@scragarDec 08.2004 — try parseing xyz into the functions or making it a global variable by having it decalred outside of a function.
Copy linkTweet thisAlerts:
@lklawlessauthorDec 08.2004 — Thanks so much for responding. Unfortunately, I don't know how to do either of those. Can you explain a bit more, please?
Copy linkTweet thisAlerts:
@scragarDec 08.2004 — <script>
var xyz = "";

// the rest of your code goes here,
// xyz however will now be available from function to function.


or alternativly change
function foo(){
to
function foo(xyz){

be carefull not to use them both though or you migh call an error.
Copy linkTweet thisAlerts:
@lklawlessauthorDec 08.2004 — Thank you so much for your help - I really appreciate this.

OK, so I took the random text array out of the function and it still works as an alert. Now can you tell me how to make the random sound play? In searching the web I've found lots of stuff about embedding sounds, but I just want a function that will tell the computer to play sound xyz that matches text xyz.

This is what I have:

[code=php]
function playSound() {

location.target='http://french.about.com/library/media/wavs/'+whichNumber+'.wav'

}
[/code]
Copy linkTweet thisAlerts:
@scragarDec 08.2004 — Oops, content cleared.
Copy linkTweet thisAlerts:
@lklawlessauthorDec 08.2004 — No, no, that's what I was saying - I want to use the same random generator for both the alert and the sound file.

I'll post my whole script so you can see what I'm talking about.

[code=php]
var whichNumber=get_random();

var number=new Array(3)
number[0]="zero";
number[1]="1";
number[2]="2";

function get_random()
{
var ranNum=Math.round(Math.random()*2);
return ranNum;
}

function showNumber() {

alert(number[whichNumber])
}

function playSound() {

location.target='http://french.about.com/library/media/wavs/'+whichNumber+'.wav'

}
[/code]


The random generator and alert work fine. It's just playSound that I'm having trouble with now.
Copy linkTweet thisAlerts:
@scragarDec 08.2004 — do you wish to play the sound in the background

document.write('<embed src="' + number[whichNumber] + '" autostart="true" loop="true" hidden="true"></embed>')

or send the user to the wav file that you want?
Copy linkTweet thisAlerts:
@lklawlessauthorDec 08.2004 — I want it to play on onClick. At the same time, I want the page to reload with the addition of the text that matches the sound file.

I'm sorry this is so complicated - I really appreciate your help.
Copy linkTweet thisAlerts:
@scragarDec 08.2004 — that's quite hard, the best method I can actualy think of would be to relode the page when they press the button but we add a query string which is read when the page loads and writes out what we want.

[code]
var number=new Array(3)
number[0]="zero";
number[1]="1";
number[2]="2";

if(window.top.location.href.indexOf("?song=") != 0){
var Play = window.top.location.href.split("song");
document.write('<embed src="' + number[Play] + '" autostart="true" loop="true" hidden="true"></embed>');
document.write("We a currently playing song " + Play);
}else{
var whichNumber=get_random();
document.write("<form method=get action=?>n");
document.write("<input type=hidden name=song value=" + whichNumber + ">");
document.write("<input type=submit value="Play a random song.">");
};

function get_random()
{
var ranNum=Math.round(Math.random()*2);
return ranNum;
}
Copy linkTweet thisAlerts:
@Warren86Dec 08.2004 — <HTML>

<Head>

<Script Language=JavaScript>

audioClip = new Object
audioClip.A = "hello.wav"
audioClip.B = "goodbye.wav"
audioClip.C = "please.wav"
audioClip.D = "thankyou.wav" // continue to add as many as you need


var nClips = new Array();
n = 0;

for (each in audioClip){nClips[n++] = audioClip[each]}

isMax = nClips.length-1;

function getClip(){

isIndex = 0+Math.round(Math.random()*isMax);
currClip = nClips[isIndex];
playIt(currClip);
}

function playIt(URL){

isSelection = URL;
embedStr = "<Embed Src="+isSelection+" Width=70 Height=40></Embed>"
document.getElementById('isEmbed').innerHTML = embedStr;
document.getElementById('isText').innerText = isSelection;
}


</Script>

</Head>

<Body>

<center>

<Div id='isText'></Div><br>

<Div id='isEmbed'></Div><br>

<input type='button' value='Play Random Clip' onclick="getClip()">

</center>

</Body>

</HTML>
Copy linkTweet thisAlerts:
@lklawlessauthorDec 08.2004 — Thanks to both of you. Scrager - I couldn't get the sound file to play with yours. Warren, it works with yours - thank you! But it shows the url of the sound file. Isn't there a way to have the url automatically generated, so that just the random number text is displayed.

In other words, I have this:

<i>
</i>audioClip = new Object
audioClip.A = "http://french.about.com/library/media/wavs/zero.wav"
audioClip.B = "http://french.about.com/library/media/wavs/1.wav"
audioClip.C = "http://french.about.com/library/media/wavs/2.wav"
audioClip.D = "http://french.about.com/library/media/wavs/3.wav"


because I didn't see anywhere else to put the path of the wavs. But I only want the name of the wav (zero, 1, 2, etc) to show up on onClick.
Copy linkTweet thisAlerts:
@Warren86Dec 08.2004 — <HTML>

<Head>

<Script Language=JavaScript>

audioClip = new Object
audioClip.A = "http://french.about.com/library/media/wavs/zero.wav"
audioClip.B = "http://french.about.com/library/media/wavs/1.wav"
audioClip.C = "http://french.about.com/library/media/wavs/2.wav"
audioClip.D = "http://french.about.com/library/media/wavs/3.wav"


var nClips = new Array();
n = 0;

for (each in audioClip){nClips[n++] = audioClip[each]}

isMax = nClips.length-1;

function getClip(){

isIndex = 0+Math.round(Math.random()*isMax);
currClip = nClips[isIndex];
playIt(currClip);
}

function playIt(URL){

isSelection = URL;
tmp = isSelection.match(/[^/\]+$/);
tmp[0] = tmp[0].split(".");
isName = tmp[0][0]
embedStr = "<Embed Src="+isSelection+" Width=70 Height=40></Embed>"
document.getElementById('isEmbed').innerHTML = embedStr;
document.getElementById('isText').innerText = isName;
}


</Script>

</Head>

<Body>

<center>

<Div id='isText'></Div><br>

<Div id='isEmbed'></Div><br>

<input type='button' value='Play Random Clip' onclick="getClip()">

</center>

</Body>

</HTML>
Copy linkTweet thisAlerts:
@lklawlessauthorDec 08.2004 — Warren, you are awesome! I can't even tell you how much I appreciate this. Many thanks!! ?
Copy linkTweet thisAlerts:
@Warren86Dec 08.2004 — You're welcome. Good luck with your project.
Copy linkTweet thisAlerts:
@lklawlessauthorDec 08.2004 — Thanks to you, my project is now done - here's the link if you're interested: [URL=http://french.about.com/library/begin/bl-numbers04.htm]http://french.about.com/library/begin/bl-numbers04.htm[/URL]

If you ever need help with French or Spanish, don't hesitate to ask. :-)
Copy linkTweet thisAlerts:
@Warren86Dec 08.2004 — I think everyone knows "feliz navidad," so how about posting a link to an audio clip demonstrating how to pronounce "Merry Christmas" in French? :-)

I am particularly interested in how to speak that in Italian, but I understand that is not your forte.
Copy linkTweet thisAlerts:
@lklawlessauthorDec 08.2004 — You're letting me off easy. :-)

[URL=http://french.about.com/library/media/wavs/joyeuxnoel.wav]Joyeux Noël[/URL]

[URL=http://italian.about.com/library/media/audio/aa12060003.wav]Buon Natale[/URL]

The Italian is from my colleague's site: [URL=http://italian.about.com]About Italian[/URL]
Copy linkTweet thisAlerts:
@Warren86Dec 08.2004 — Fantastic!

I have a dinner to attend and the host is Italian, and I really wanted to at least say some kind of greeting, other than: "Hey, paisano, que ore sono?"

Que serra, serra

Take care.
Copy linkTweet thisAlerts:
@Warren86Dec 08.2004 — Molto bene, bella bellisima.
×

Success!

Help @lklawless spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...