How to create a function for a hint button in code.org

511 Views Asked by At

How do I create a function for my hangman game in code.org?

I need to make a "Hint" button, so that when I press the button, the hint that goes to the word you're going to guess, gives you a clue.

Example

My word is abandon and the clue would say following when I click on the "Hint" button:

To cease to support or look after (someone); desert

I already made the variables for the guess words in the hangman game like:

var words= ["abandon", "above", "about"];
var word = "";
1

There are 1 best solutions below

3
On

I made a little mock up of what your describing. Since Code.org is have design and half code, note my design. Your can interact with my design sample hangman program.

Basic AppLab screen design with text entry, hint label, and three buttons

I made a corresponding list hints. I then select a random number and store in index. This tells me which spot in words and hints to use.

I then implement my three buttons. The first, hintBtn, sets the text of the label, and unhides it (it was hidden as part of DesignMode).

The second button, newWordBtn, picks a different index to use as the answer. It also makes the hint hidden again (we change the text of the hint label when the user clicks hintBtn).

I've left the guessBtn for you to fully implement.

//Code.org Project: https://studio.code.org/projects/applab/38gmbu9k7LCiyPIRALOnHVQgEKVQ1PuPkC7hXmOGc3k
var words= ["abandon", "above", "apple"];
var hints = ["to be left without warning", "not below", "Washington's fruit"];

var index = randomNumber(0, words.length - 1);

var word = words[index];

onEvent("hintBtn", "click", function( ) {
  //this code will run when "Hint" clicked
  //will grab the hint from the hints list 
  //  based on what spot using right now ('index')
  setProperty("hintText", "text", hints[index]);
  //reveal the hint (want to change the text first)
  setProperty("hintText", "hidden", false);
});

onEvent("newWordBtn", "click", function( ) {
  //change which word spot using
  index = randomNumber(0, words.length - 1); 
  //rehide the hint text
  setProperty("hintText", "hidden", true);
});

onEvent("guessBtn", "click", function( ) {
  //to implement
  console.log(word);
});

Hope this helps.