2017-05-06 96 views
-4

我想顯示一些空白字母空格數=從數組中抽取的隨機單詞的字母數。在JavaScript中 - 顯示一個字符串乘以一個變量

這是一個類項目的hang子手遊戲。我有隨機拉的單詞和該單詞中的字母數量,但試圖使用我分配了該數字的變量也證明有點棘手。

任何幫助表示讚賞!

+2

你有什麼代碼,使遠嗎? – mayersdesign

+2

您可以發佈您嘗試過的任何代碼嗎? – Sreekanth

+2

歡迎來到[so]!在這個網站,你應該嘗試**自己編寫代碼**。後** [做更多的研究](//meta.stackoverflow.com/questions/261592)**如果你有問題,你可以**發佈你已經嘗試**與清晰的解釋是什麼是'工作**並提供[** Minimal,Complete和Verifiable示例**](// stackoverflow.com/help/mcve)。我建議閱讀[問]一個好問題和[完美問題](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)。另外,一定要參加[遊覽]並閱讀[this](// meta.stackoverflow.com/questions/347937/)**。 –

回答

0

你可以試試下面的代碼:

// 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(" ") 
相關問題