【问题标题】:In JavaScript- Display a string multiplied by a variable [duplicate]在 JavaScript 中 - 显示一个字符串乘以一个变量 [重复]
【发布时间】:2017-10-04 15:36:54
【问题描述】:

我想显示一些空白字母空格 = 我从数组中提取的随机单词的字母数。

这是针对班级项目的刽子手游戏。我有随机抽取的单词和该单词中的字母数,但尝试使用我分配给该数字的变量也证明有点棘手。

任何帮助表示赞赏!

【问题讨论】:

标签: javascript


【解决方案1】:

你可以试试下面的代码:

// 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(" ")

【讨论】:

    猜你喜欢
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    • 2018-04-13
    • 2011-05-14
    • 1970-01-01
    • 2010-12-25
    相关资源
    最近更新 更多