我怎样才能为多个值使用单个var?

时间:2016-01-13 21:27:47

标签: javascript

我正在尝试获取随机值,获取随机数组元素。 当我生成它们时,它们是相同的问题。 这可能是一个愚蠢的问题,但我如何获得每个实例的随机值。  我的HTML看起来像这样

Our Array consists of:<br>
<span id="massParts"></span><br><br>
Generating a word from syllables:<br>
<span id="oneWord"></span><br><br>
Generating a sentence from several words<br>
<span id="oneSentence"></span>

我想要实现的是获得随机生成的单词的句子。我只提出创建多个单词然后将它们组合在一起的解决方案。但这不是更大文本的解决方案。

我的脚本是:

<script>
    // creating an array of syllables
var parts = ["ing", "er", "a", "on", "po", "i", "re", "tion"];
var partsAsString = parts.join(', ');

    // display syllables array elements
document.getElementById("massParts").innerHTML = partsAsString;

    // getting random element from an array
var a = Math.floor(Math.random() * parts.length);
var b = Math.floor(Math.random() * parts.length);
var c = Math.floor(Math.random() * parts.length);

    // making a word with one, two and three syllables
var oneSylWord = parts[a];
var twoSylWord = parts[a]+parts[b];
var threeSylWord = parts[a]+parts[b]+parts[c];

    //putting three words into an array
var newWord = [oneSylWord, twoSylWord, threeSylWord];

    // taking one of those free words fron the new array
var randWord = newWord[Math.floor(Math.random() * newWord.length)];

    // display a random 1,2,3 syllable word
document.getElementById("oneWord").innerHTML = randWord;

   // generating a sentence
var sentence = randWord + " " +  randWord +  " " +  randWord + ".";
document.getElementById("oneSentence").innerHTML = sentence;
</script>

我知道我会显示相同的var randWord并导致重复。但我怎样才能避免使用多个变量。

谢谢。

这是一个jsfiddle https://jsfiddle.net/2j9jpcoo/

3 个答案:

答案 0 :(得分:2)

将代码放入function并多次调用以在每次调用时获得(可能)不同的值:

function getRandomValue(arr);
  return arr[Math.floor(Math.random() * arr.length)];
}


var sentence = getRandomValue(newWord) + " " +  getRandomValue(newWord) +  " " +  getRandomValue(newWord) + ".";

函数允许您组织和重用代码。从以上链接:

  

函数是JavaScript编程的基础。将一段程序包装在一个值中的概念有很多用途。它是一个工具,用于构建更大的程序,减少重复,将名称与子程序相关联,以及将这些子程序相互隔离。

答案 1 :(得分:0)

我喜欢以下算法生成随机数,根据this article,它提到它在性能上更好但是老实说我没有对它进行基准测试,但你可以在jsfiddle demo中查看,只需运行它并查看基准测试结果

// the initial seed
Math.seed = 6;

// in order to work 'Math.seed' must NOT be undefined,
// so in any case, you HAVE to provide a Math.seed
Math.seededRandom = function(max, min) {
    max = max || 1;
    min = min || 0;

    Math.seed = (Math.seed * 9301 + 49297) % 233280;
    var rnd = Math.seed / 233280;

    return min + rnd * (max - min);
}

答案 2 :(得分:0)

我重新组织了你的代码并创建了一些可重复使用的函数,每次都得到一个不同的随机字。在这种情况下,我们可以跳过变量并直接使用函数的返回值:

// creating an array of syllables
var a, b, c, newWord;
var parts = ["ing", "er", "a", "on", "po", "i", "re", "tion", "con", "de", "sta"];
var partsAsString = parts.join(', ');

// display syllables array elements
document.getElementById("massParts").innerHTML = partsAsString;

function shuffleLetters() {
  // getting random element from an array
  a = Math.floor(Math.random() * parts.length);
  b = Math.floor(Math.random() * parts.length);
  c = Math.floor(Math.random() * parts.length);

  var oneSylWord = parts[a];
  var twoSylWord = parts[a]+parts[b];
  var threeSylWord = parts[a]+parts[b]+parts[c];

  //putting three words into an array
  newWord = [oneSylWord, twoSylWord, threeSylWord];
}

function getRandomWord() {
  shuffleLetters();

  // taking one of those free words fron the new array
  return newWord[Math.floor(Math.random() * newWord.length)];
}

// making a word with one, two and three syllables
shuffleLetters();

// display a random 1,2,3 syllable word
document.getElementById("oneWord").innerHTML = getRandomWord();

// generating a sentence
var sentence = getRandomWord() + " " +  getRandomWord() +  " " +  getRandomWord() + ".";

document.getElementById("oneSentence").innerHTML = sentence;