...

View Full Version : Generating random word with function - but need to keep the exact word for another



pixo_newb
03-25-2012, 04:48 PM
Sorry about the title, I didn't know what else to use to describe my problem.

Basically, I'm generating a random word with a function; then I'm trying to pass this word down to another function. The problem is, when I pass the word, it changes each time (due to it being randomly generated originally).

I'm only calling the function once (via button click), but I'm also calling the function in my code lower down to retrieve the returned variable; and the function seems to be running again and returning a new word from my array.

Here's a snippet of my code:


function ranNum(){
var ranNum = Math.round(Math.random()*10);
var chosenWord = wordArray[ranNum];
return chosenWord;
}

function makeBoxes(x){
//remove children when new word is chosen
var hM = document.getElementById("hangMan");
while(hM.firstChild){
hM.removeChild(hM.firstChild);
}

var chosenWord = ranNum(x);
var wL = chosenWord.length;

//create box for length of letters in word
var i = 0;
for(i=0;i<wL;i++){
var cBoxes = document.createElement("div");
cBoxes.className = "letterBoxes";
cBoxes.innerHTML = chosenWord.charAt(i);
hangMan.appendChild(cBoxes);
}

return chosenWord;
}

function checkLetter(y){
var chosenWord = makeBoxes(y);
alert(chosenWord);
}


So I generate a word with one button; now I need to be able to work with said word in my checkLetter() function. The word changes however.

Any help would be greatly received.

pixo_newb
03-25-2012, 05:35 PM
To clarify, I'm having a problem passing a function a variable - the function I'm passing to actually runs the function I'm trying to retrieve the variable from.

xelawho
03-25-2012, 07:06 PM
I am guessing that you need to store your variable as a global so as to be able to access it from another function without calling the originating function again. If you post your full code it would be easier to see what's going on

pixo_newb
03-25-2012, 07:15 PM
I am guessing that you need to store your variable as a global so as to be able to access it from another function without calling the originating function again. If you post your full code it would be easier to see what's going on

Thanks, I know I can do that but I read it's best to avoid using globals if possible.

There is no other way to simply pass one variable to another function without calling the function? I thought this was what return (variable) would be used for, but seems not.

Thanks though.

xelawho
03-25-2012, 08:40 PM
the other way is to pass the variable as an argument, which I didn't mention because you are already doing that:



<head>
<script type="text/javascript">
function setVar(){
var myvar="hi there";
showVar(myvar)
}

function showVar(arg){
alert(arg);
}
</script>

</head>
<body>
<input type="button" value="set variable" onclick="setVar()"/>
</body>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum