-3

I'd like to display a number of blank letter spaces = to the number of letters of a random word I pull from an array.

This is for a hangman game for a class project. I have the randomly pulled word and the number of letters in that word but trying to use the variable that I've assigned that number too is proving a bit tricky.

Any help appreciated!

4
  • 2
    What code do you have so far? Commented May 6, 2017 at 5:28
  • 2
    Can you please post any code that you have tried? Commented May 6, 2017 at 5:28
  • 2
    Welcome to Stack Overflow! At this site you are expected to try to write the code yourself. After doing more research if you have a problem you can post what you've tried with a clear explanation of what isn't working and providing a Minimal, Complete, and Verifiable example. I suggest reading How to Ask a good question and the perfect question. Also, be sure to take the tour and read this. Commented May 6, 2017 at 5:28
  • 2
    Wow that was quick. Will update with relevant code asap. Commented May 6, 2017 at 5:34

1 Answer 1

0

You can try the following code:

// The array of words you have
var $words = [
    'Totidem',
    'Pugnabant',
    'Calidis',
    'Circumfluus',
    'Formaeque'
];
// Pick a random array index
var $idx = Math.floor( Math.random() * $words.length );
// Get the word in the index equal to $idx
var $word = $words[$idx];
// Generate the repeated string. In case you like to display a different
// character, replace the `.join(" ")` with the character you need. For 
// example `.join("_")` or `.join("_=_")`
var $repeated_string = Array( 1 + $word.length).join(" ")
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.