var areYouReady = prompt("What do you want to do?").toLowerCase();
var starting_DescriptionStart;
var starting_DescriptorOfThing;
var starting_Thing;
var starting_DescriptionEnd;
if(areYouReady === "generate"){
//Just runs the generator
starting_DescriptionStart = ["You see a"];
starting_DescriptorOfThing = ["Large", "Small", "Red", "Blue", "Green", "Black", "White", "Invisible", "Camo", "Striped", "Breathing", "Living", "Sentient"];
starting_Thing = ["Chest", "Snake", "Spider", "Bird", "Bull", "Minotaur", "Sword", "Necklace", "Amulet", "Crown", "Pile of coins", "Deer", "Bunny", "Person", "Goblin", "Orc", "Wizard", "Board", "Ketchup"];
starting_DescriptionEnd = ["that wants to kill you.", "in a desert.", "on youtube.", "in a dark dungeon.", "in your neighbors house.", "underground.", "in a tree.", "in a restaurant.", ""];
// Random numbers are made
var randomNumber1 = parseInt(Math.random() * starting_DescriptionStart.length);
var randomNumber2 = parseInt(Math.random() * starting_DescriptorOfThing.length);
var randomNumber3 = parseInt(Math.random() * starting_Thing.length);
var randomNumber4 = parseInt(Math.random() * starting_DescriptionEnd.length);
generatedArea = starting_DescriptionStart[randomNumber1] + " " + starting_DescriptorOfThing[randomNumber2] + " " + starting_Thing[randomNumber3] + " " + starting_DescriptionEnd[randomNumber4];
alert(generatedArea);
}else if(areYouReady === "add description start"){
//Lets you add a descriptionStart
var s_DescStart = starting_DescriptionStart.length + 1;
var inputA = prompt("What do you want to add to the description start");
starting_DescriptionStart[s_DescStart] = inputA;
alert(starting_DescriptionStart[1]);
}

我有这个代码,只需添加到数组就可以轻松添加,但是如果我想创建一个允许用户添加它的代码,然后运行它。
运行时出现此错误,输入"添加说明开始"
未捕获的TypeError:无法读取属性'长度'未定义的
但是,我不知道为什么它说它是一个全局变量时未定义。
答案 0 :(得分:1)
在else if
声明中,您没有以任何方式定义starting_DescriptionStart
。它仍未定义,因此它没有长度。
starting_DescriptionStart
在代码开头被声明为变量,仅在if
语句中定义。