12 Replies - 1033 Views - Last Post: 15 May 2011 - 06:40 PM

#1 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Call the returned value of a function without calling the function

Posted 13 May 2011 - 01:13 PM

How do. I am creating a little word guess game, with a random function which picks the word from an array of 10 words. The second function checks if the users' letter choice is part of the secret word. Currently, each time the checkGuess() function is called, the word is changed, probably because I am calling the wordPicker() function from within. The wordPicker randomly chooses the word, then returns that word. All I want to do is pull that word into the checkGuess function, without calling the wordPicker function as it currently does. Here is the code:

		// Create secret word array
		var wordList = new Array("stealth", "telephone", "internet", "nickel", "marine", "instantiate", "method", "function", "television", "monitor")
		
		// Randomly pick the word from the word list
		function wordPicker ()
		{
			// Create a random number
			var rand = Math.floor(Math.random()*10)
			
			var word = wordList[rand];
//			document.forms[0].secret.value = word;
			
			return word;
		}
			
		function checkGuess()
		{
			// Get users guess
			var guess = document.forms[0].letter.value.toLowerCase();
			
			// Put the secret word in a variable so it can be used
			var progress = document.forms[0].secretWord.value;
			
			// Reset the guess field
			document.forms[0].letter.value = "";
			document.forms[0].letter.focus();
			
			// Convert secret word to an array
			var progressArray = new Array();
			progressArray = progress.split("");
			
			// Convert guess word to an array
			var secretWordArray = new Array();
			var secret = wordPicker();
			
			window.alert(secret);
			
			secretWordArray = secret.split("");
			
			// See if the guess is correct
			for (var i=0; i < guessLetterArray.length; i++)
			{
				var curLetter = secretWordArray[i];
				if (curLetter == guess) 
				{
					guessLetterArray[i] = guess;
				}
			}
			
			// Put the guess back together
			progress = progressArray.join("");
			
			var answer = "Correct! The word is " + progress;
			
			if (progress == secret)
			{
				document.forms[0].secretWord.value = answer;
			}
			else
			{
				document.forms[0].secretWord.value = progress;
			}
		}	
		
    /* ]]> */
    </script>
<title>Guessing Game</title>
</head>

<body>
<div id="container">
<h1>Guessing Game</h1>
<form action="" method="get" enctype="application/x-www-form-urlencoded" onsubmit="checkGuess()">
<p> Mystery word:
<input type="text" name="secretWord" value="**********" readonly="readonly" />
</p>
<p>Enter a letter and click the Try Letter button.</p>
<p><input type="text" name="letter" size="1" />
<input type="submit" value="Try Letter" /></p>
<input type="button" value="New Word" onclick="wordPicker()"/>
</form>
</div>
</body>

</html>




Is This A Good Question/Topic? 0
  • +

Replies To: Call the returned value of a function without calling the function

#2 Prototypical  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: Call the returned value of a function without calling the function

Posted 13 May 2011 - 01:25 PM

I'm confused - why are you calling wordPicker ? If wordPicker gives you a random word -- potentially different than the one you currently have - why would you call it? What is the logical reason to do so ?
Was This Post Helpful? 0
  • +
  • -

#3 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: Call the returned value of a function without calling the function

Posted 13 May 2011 - 01:29 PM

The theory was that I need the word that wordPicker chose, so i can use it in checkGuess, and that is the end result I am trying for at line 34 of the code. I also tried:

34 var secret = word;

but the didn't work at all. So basically, I need to know how to get the word from wordPicker, into checkGuess.
Was This Post Helpful? 0
  • +
  • -

#4 Prototypical  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: Call the returned value of a function without calling the function

Posted 13 May 2011 - 01:41 PM

Think about this logically.

wordPicker is a function that returns a word.

A different word each time.

Should you be changing the secret word every time a guess is made ?

Forget about code -- think about the logic.

Also, do you intend to be reloading the page each time you guess a letter ? How will the next page know what the secret word is ?

This topic is pretty much asking someone to think/program for you.

Why don't you jot down some psuedo code here for us about how this page should work. Evaluate it first on that level, and then use that as your blueprint to code.

This post has been edited by Prototypical: 13 May 2011 - 01:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: Call the returned value of a function without calling the function

Posted 13 May 2011 - 02:22 PM

Prototype, you are correct that I am having a little trouble wrapping my head around the logic, and maybe some confusion as to variable scope. I know I need to get the word returned from wordPicker into a variable within checkGuess so i can compare the users guesses, but I am not sure how to do that syntactically. Here is my psuedo:

Function-----
Create an array of ten words
Randomly choose one word from the array
Return that word


Function----
Take the returned word
Put into an array
Take users input (single letter)
Put into an array
See if users letter is contained in the returned word array
If so, put the letter into a new array of successful guesses
If not, discard the letter
Check if the successful guess matches the secret word
If so, display the successful guesses in textfield, so * appear in place of un guessed letters
Was This Post Helpful? 0
  • +
  • -

#6 Prototypical  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: Call the returned value of a function without calling the function

Posted 13 May 2011 - 02:50 PM

The concept of using wordPicker in your checkGuesses function, is not syntax, it's about logic.

You should only call wordPicker once per round. Right ?

It should not be called every time you guess a letter.

The flaw in your logic is that you aren't thinking about storing the word returned by wordPicker and using it for the duration of the round to check against guesses.

Right ?
Was This Post Helpful? 0
  • +
  • -

#7 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: Call the returned value of a function without calling the function

Posted 13 May 2011 - 03:21 PM

That is EXACTLY what I am trying to do, use the same word for the round, which is why I dont want to call wordPicker within checkGuess, because that is what is changing the word.

You mention that my error is not with syntax, does that mean I have the correct code, but I am incorrectly executing it?
Was This Post Helpful? 0
  • +
  • -

#8 GhandiJones  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 54
  • View blog
  • Posts: 174
  • Joined: 17-March 11

Re: Call the returned value of a function without calling the function

Posted 13 May 2011 - 03:38 PM

You need to have wordpicker create a variable. And then you check against said variable during the remainder of the round.

This post has been edited by GhandiJones: 13 May 2011 - 03:40 PM

Was This Post Helpful? 0
  • +
  • -

#9 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: Call the returned value of a function without calling the function

Posted 13 May 2011 - 04:04 PM

Ghandi, i tried that, by changing like 34 to read:

var secret = word;

wordPicker returns the variable 'word', which I then pulled into checkGuess with the above line, but the error console says "word is undefined" at line 34.
Was This Post Helpful? 0
  • +
  • -

#10 Prototypical  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: Call the returned value of a function without calling the function

Posted 13 May 2011 - 04:36 PM

I think Ghandi's code really was quite accurate. :) enlightening even.

The error you got was completely correct. The variable word doesn't exist except in the scope of the wordPicker function.

This post has been edited by Prototypical: 13 May 2011 - 04:36 PM

Was This Post Helpful? 0
  • +
  • -

#11 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: Call the returned value of a function without calling the function

Posted 15 May 2011 - 05:19 PM

I changed the declaration of the 'word' variable in the wordPicker function to a global, but the error console still shows it as undefined. I am really not trying to get anyone to do my code for me, it's just that I have only been learning JS for about 6 weeks, and I hate to say it, but I don't know everything.

Logically speaking, it makes sense to me that the variable word, declared as a gloabel within the first function, should be available for use in the second function.. is that not correct?
Was This Post Helpful? 0
  • +
  • -

#12 Prototypical  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: Call the returned value of a function without calling the function

Posted 15 May 2011 - 06:24 PM

I do understand that you are learning.

When you have a function that returns a value, the whole logical concept behind that is that what is inside the function is not global.

The problem is that you are trying to do step 5, when you don't fully understand step 1. So it's not going to make sense.

In javascript anything inside a function, does not get executed, until you actually call the function.

Everything not in a function will be executed when the the page is loaded.

That would be a good place to put something like

var currentSecretWord = wordPicker();


if you do that.. you now have the global variable currenSecretWord assigned a random word via your wordPicker function.

Do you understand that ? if so, apply it.


I see many design issues with your current code, and I strongly urge you to pick up a javascript book and take the time to learn about functions, variables, and scope.

There is NOTHING that I have said so far or will probably say in this thread that isn't explained in great detail in a book about learning javascript.

This post has been edited by Prototypical: 15 May 2011 - 06:31 PM

Was This Post Helpful? 1
  • +
  • -

#13 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: Call the returned value of a function without calling the function

Posted 15 May 2011 - 06:40 PM

Cool. It works now, thank you. I will take your advice.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1