Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    28
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Generating random word with function - but need to keep the exact word for another

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

  • #2
    New Coder
    Join Date
    Nov 2011
    Posts
    28
    Thanks
    13
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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

  • Users who have thanked xelawho for this post:

    pixo_newb (03-25-2012)

  • #4
    New Coder
    Join Date
    Nov 2011
    Posts
    28
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho View Post
    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.

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    the other way is to pass the variable as an argument, which I didn't mention because you are already doing that:

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

  • Users who have thanked xelawho for this post:

    pixo_newb (03-25-2012)


  •  

    Posting Permissions

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