-2

I'm working on a JavaScript Hangman game and I need to print out the same number of dashes as the length of the word chosen at random. Iv'e done that but it also prints out a dash for the gaps that are in the sentence, so how can I ignore these spaces and only print out a dash for the letters in the sentence? Any help appreciated Thanks.

This is the code that prints the dashes out

for(var i = 0; i < sayings[randomSaying].length; i++)
{
   progress.innerHTML += "-";
}

Random Variable

var sayings = [
    "cash on the nail",
    "charley horse",
    "foul play",
    "bury the hatchet",
    "hands down",
    "if the cap fits",
    "mumbo jumbo",
    "see red",
    "stone the crows",
    "thick and thin",
]
sayings.toString();
var randomSaying = Math.floor(Math.random()*sayings.length);
seanrs97
  • 323
  • 3
  • 14

3 Answers3

2

You don't need to ignore the spaces, just replace the letters

progress.innerHTML = sayings[randomSaying].replace(/[a-z]+/g, "-");

Single line of code, all in one line, more efficient than looping ;)

4castle
  • 32,613
  • 11
  • 69
  • 106
David Espino
  • 2,177
  • 14
  • 21
  • 1
    `/\S+/g` or `/[^ ]+/g` would replace any non-space character. Or `/\w+/g` could be used if you just expect alphanumeric sayings. – 4castle May 19 '16 at 01:12
  • Thankyou for your answer! – seanrs97 May 19 '16 at 01:13
  • @4castle is right... but since your code is just showing `a-z` non Capital, I just put `a-z` in the regExp :). You can always tune that up to your needs. – David Espino May 19 '16 at 01:14
  • @4castle Have you ever played hangman where you were supposed to fill in anything but letters? – Barmar May 19 '16 at 01:18
  • @4castle I found this, in case you're interested :D http://stackoverflow.com/questions/6243563/regex-vs-while-loops – David Espino May 19 '16 at 01:18
  • @Barmar Yes I have. @DavidEspino That post says that regex is only faster if its using a `$` to anchor the search. In this case both the regex and loop would iterate the same amount and with the same speed. I agree though that the regex solution is nicer, but there's no efficiency involved. – 4castle May 19 '16 at 01:38
  • 1
    I just noticed that the `+` is replacing each entire word with one dash `-` instead of replacing each individual letter. Remove the `+`. – 4castle May 19 '16 at 01:45
1

Use an if statement within your for loop. Also, don't use innerHTML += because it causes the browser to have to re-parse the DOM over and over. Do this instead:

var hiddenSaying = "";
for(var i = 0; i < sayings[randomSaying].length; i++) {
    if (sayings[randomSaying].charAt(i) === ' ')
        hiddenSaying += " ";
    else
        hiddenSaying += "-";
}
progress.innerHTML = hiddenSaying;
4castle
  • 32,613
  • 11
  • 69
  • 106
  • You're right, it's just an opinion :)... will remove the comment. Personally I prefer to write less code to achieve the results ... sorry about it – David Espino May 19 '16 at 01:11
1

Can't say for sure, but I think you should take a look at this:

function randomSaying(sayingsArray){
  var p = sayingsArray[Math.floor(Math.random()*(sayingsArray.length+1))];
  var d = p.replace(/\s/g, '').replace(/./g, '&#8208;');
  return {phrase:p, dashes:d};
}
var obj = randomSaying(sayings);
// obj.phrase is phrase obj.dashes is dashes
StackSlave
  • 10,613
  • 2
  • 18
  • 35