我正在尝试在JS中创建这个随机名称生成器。数组返回undefined?

时间:2012-06-17 01:44:15

标签: javascript arrays random

我正在尝试创建一个简单的生成器,它从用户那里获取一些音节,然后从中随机形成一个组成的单词。

我设法制作了一个不错的输入系统。它将音节放入2D数组中。我已经对它进行了测试,但它确实有效。

当我试图提醒数组的随机部分时,它对我大喊:“未捕获的TypeError:无法读取未定义的属性'0'”

<html>
<head>
    <script type = "text/javascript">
        var sounds = [[],[],[]];

        function makeName(x){
            alert(x[0][random(x[0].length)] + x[1][random(x[1].length)] + x[2][random(x[2].length)]); //Something is wrong here
        }

        function random(x){ //Random number between 0 and x-1
            return Math.floor(Math.random()*x); //The problem might be here too
        }

        var Count0 = 0;
        var Count1 = 0;
        var Count2 = 0;

        function add(type, fooNum) {

            //Create an input type
            var element = document.createElement("input");

            //Add to index of user supplied data
            if(fooNum == 1){
                Count0++;
                element.setAttribute("id","0:" + Count0);
            }else if(fooNum == 2){
                Count1++;
                element.setAttribute("id","1:" + Count1);
            }else if(fooNum == 3){
                Count2++;
                element.setAttribute("id","2:" + Count2);
            }else{
                alert("Somethin' went wrong, man!");
            }

            console.log(element.id);

            //Assign different attributes to the element.
            element.setAttribute("type", type);
            element.setAttribute("value", "");
            var foo = document.getElementById("fooBar"+fooNum);

            //Append the element in page (in <span>).
            foo.appendChild(element);

        }

        function generate(){ //Assign the inputs to a 2D array
            var counts = [Count0, Count1, Count2];
            hello=counts;
            for(k=0; k<=2; k++){
                for(i=0; i<=counts[k]; i++){
                        sounds[k][i] = document.getElementById(k + ":" + i).value;
                        alert(sounds[k][i]);
                }
                i = 0;
            }
        }

    </script>
</head>
<body>
    <FORM>

        <INPUT type="button" value="Add" onclick="add(document.forms[0].element.value, 1)"/><INPUT type="text" name="element" id="0:0"/><span id="fooBar1">&nbsp;</span><P>
        <INPUT type="button" value="Add" onclick="add(document.forms[0].element.value, 2)"/><INPUT type="text" name="element" id="1:0"/><span id="fooBar2">&nbsp;</span><P>
        <INPUT type="button" value="Add" onclick="add(document.forms[0].element.value, 3)"/><INPUT type="text" name="element" id="2:0"/><span id="fooBar3">&nbsp;</span><P>

        <INPUT type="button" value="Generate" onclick="generate()"/><P>
        <INPUT type="button" value="Make Name" onclick="makeName()"/><P>
    </FORM>
</body>

我的猜测是问题在于random()或makeName()。

请帮忙!提前谢谢。

2 个答案:

答案 0 :(得分:3)

您的makeName函数接受参数x

 function makeName(x) {

但你这样称呼它:

<INPUT type="button" value="Make Name" onclick="makeName()"/>

这就是xmakeName未定义的原因。

答案 1 :(得分:1)

在你的html中你没有任何参数调用makeName函数

<INPUT type="button" value="Make Name" onclick="makeName()"/><P>

但你的功能需要一个参数

function makeName(x){
    alert(x[0][random(x[0].length)] + x[1][random(x[1].length)] + x[2][random(x[2].length)]);
}

因此x函数中未定义makeName