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 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting the value of a variable from another function.

    Heres what i got, ignore all the unknowns they come from textboxes etc.

    My problem is in this function I change the value of a variable. I want to store that variable change so i can use it in another function. Heres what i got.

    Code:
    	function setupGame(){
    	var guessesLeft = guessNo.value = 10;
    	var wordSplice = new Array();
    	wordVal = word.value
    	wordVal = wordVal.toLowerCase();
    	alert(wordVal);
    		for(var i=0; i<wordVal.length; i++){
    			wordSplice[i] = wordVal.substring(i, i+1);
    			wordSoFar.value += "*"
    		}		
    	}
    Thats the first function, the variable or 'Array' values of wordSplice I saved I want to use in this function :

    Code:
    function runCoreEngine(){
    	for(i in wordSplice){
    	}
    }
    Really appreciate any help, if theres anything I was unclear on let me know I really need a solution. Cheers!

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    That variable should be global. Declare it outside the function.
    Code:
    var wordSplice;
    function setupGame(){
    	var guessesLeft = guessNo.value = 10;
    	wordSplice = new Array();
    	wordVal = word.value
    	wordVal = wordVal.toLowerCase();
    	alert(wordVal);
    	for(var i=0; i<wordVal.length; i++){
    		wordSplice[i] = wordVal.substring(i, i+1);
    		wordSoFar.value += "*"
    	}		
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey Thanks it worked, it was weird I tried that originally, didint work but then i added "= new Array()" to the end of the global one it worked.


    Cheers mate.


  •  

    Posting Permissions

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