-1

I have this list:

let teams = [
  "real madrid",
  "barcelona",
  "milan",
  "inter",
  "juventus",
  "manchester united",
  "manchester city",
  "liverpool",
  "arsenal",
  "chelsea",
  "bayern munich",
];

I wrote code for choose random item from this list but I want display "-" as long as item length.

For example: choose real madrid ... I want this - - - - - - - - - - in the text box.

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
Amir Rezaie
  • 43
  • 1
  • 11
  • You forgot one `"-"` for the space between *Real* and *Madrid* — or you didn't? What have you tried? Please [edit] with a [mcve]. if still in doubt, read [ask]. – Roko C. Buljan Jan 17 '22 at 21:56

1 Answers1

1

Use String.prototype.replace() with a small RegExp that will match any non-space character \S, and replace it with a hyphen - (PS: g stands for global replace)

const str = "Real Madrid";

const hyphenated = str.replace(/\S/g, "-");
console.log(hyphenated);

Happy hangman-ing!

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313