0

nameGenerator()変数、をパラメータとして受け取るという関数がcategoryあります。

この関数を定義する前に、ワードリスト配列の2つの「ペア」があります。djentWords1djentWords2、次にhardcoreWords1、とhardcoreWords2です。

nameGenerator()次の変数を定義します。

  1. firstNumsecondNum
  2. firstWordsecondWord
  3. bandName

この関数は、0と&、または&のいずれかの長さの間に2つの乱数(firstNumおよび)を生成します。私の質問は、「djent」や「hardcore」などのパラメーターを渡すことができ、そのパラメーターに基づいて、適切な配列の長さを使用して乱数を生成できるかどうかです。そのままの関数は次のとおりです。secondNumdjentWords1djentWords2hardcoreWords1hardcoreWords2nameGenerator()

//First category: djent
var djentWords1 = ["Aman", "Soul", "Cloud", "Calculate", "Pythagoran"];
var djentWords2 = ["NaaKi", "Circlet", "Cykul", "Consciousness", "Daaka"];

//Second category: hardcore
var hardcoreWords1 = ["SMASH", "RAGE", "LIFE", "THESE", "FIRST", "BRASS", "LAST"];
var hardcoreWords2 = ["FIST", "FIGHTER", "BREAKER", "SMASHER", "RUINER", "DAYS", "CHANCE"];


function nameGenerator (category){
    //Randomize
    var firstNum = Math.floor(Math.random() * categoryWords1.length); //categoryWords1 would either be djentWords1 or hardcoreWords1, based on the parameter passed to the function
    var secondNum = Math.floor(Math.random() * categoryWords2.length); //categoryWords2 would either be djentWords2 or hardcoreWords2, based on the parameter passed to the function
    var firstWord = categoryWords1[firstNum]; //firstWord = the word whose position corresponds to the first randomly-generated number
    var secondWord = categoryWords2[secondNum]; //secondWord = the word whose position corresponds to the second randomly-generated number
    var bandName = firstWord + secondWord;
}

事前に感謝します-そしてうまくいけば、これはそれほど混乱していませんでした。すべての助けは大歓迎です。

4

2 に答える 2

3

オブジェクト(連想配列)を使ってみませんか?

var words = {
    djent: [
        ["Aman","Soul","..."],
        ["NaaKi","Circlet","..."]
    ],
    hardcore: [
        ["..."],
        ["..."]
    ]
};
function nameGenerator(category) {
    var bandName = words[category][0][Math.floor(Math.random()*words[category][0].length)]
       + words[category][1][Math.floor(Math.random()*words[category][1].length)];
    return bandName;
}
于 2013-02-19T03:01:16.917 に答える
0

これらの変数を配列属性として宣言する必要があります。これを試して :

var djent = {
    words1 : ["Aman", "Soul", "Cloud", "Calculate", "Pythagoran"],
    words2 : ["NaaKi", "Circlet", "Cykul", "Consciousness", "Daaka"]
}
var hardcore = {
    words1 : ["SMASH", "RAGE", "LIFE", "THESE", "FIRST", "BRASS", "LAST"],
    words2 : ["FIST", "FIGHTER", "BREAKER", "SMASHER", "RUINER", "DAYS", "CHANCE"]
}

function nameGenerator (category){
    var firstNum = Math.floor(Math.random() * window[category].words1.length)
       , secondNum = Math.floor(Math.random() * window[category].words2.length)
       , firstWord = window[category].words1[firstNum]
       , secondWord = window[category].words2[secondNum]
       , bandName = firstWord + secondWord;
    return bandName;
}
于 2013-02-19T03:10:50.367 に答える