【发布时间】:2017-07-24 09:21:09
【问题描述】:
我正在尝试在循环中执行一条语句,前提是不满足所有先前的条件并且不返回(结束迭代)。但是,即使循环显然已经结束了它的迭代,该语句仍在执行。
这一切都发生在“分类单词”部分的“解析”方法中。如果其他条件不适用,我只想执行的语句是“添加未分类”语句。我没有包括包含“项目”的对象,因为它有很多其他你不需要看到的东西。但如果你这样做,可以在这里找到:Link
var textParser4 = {
// It need to store command phrases
commandWords: ["north", "pick up", "east", "south", "west", "up", "down", "examine", "open", "inventory", "use", "get", "take", "drop", "put dn"],
// It needs to store filler words - to remove
fillerWords: ["go", "to", "then", "with", "it", "at", "as", "and", "i"],
// It needs to store string after it's turned into array
stringArray: [],
// It needs somewhere to store seperated strings
seperateStringArrays: [],
// It needs a parsing function
parse: function(inputString) {
// It needs to make input string lower case.
inputString = inputString.toLowerCase();
// It needs to remove punctuation
inputString = inputString.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
// It needs to remove filler words.
this.fillerWords.forEach(function(word) {
inputString = inputString.replace(word + " ", "");
});
// Turn string into array
this.stringArray = inputString.split(" ");
// Remove empty elements
this.stringArray.forEach(function(element, index) {
if (element === "") {
this.stringArray.splice(index, 1);
}
}, this);
// Catagorise words
for (var i = 0; i < this.stringArray.length; i++) {
console.log("string array length: ", this.stringArray.length);
var jWord = this.stringArray[i] + " " + this.stringArray[i + 1];
this.commandWords.forEach(function(cWord) {
//find two word commands first
if (cWord === jWord) {
this.seperateStringArrays.push({ phrase: this.stringArray[i] + " " + this.stringArray[i + 1], wordCatagory: "command" });
// Remove word that has been joined with previous word
this.stringArray.splice(i + 1, 1);
return;
// find single word commands
} else if (cWord === this.stringArray[i]) {
this.seperateStringArrays.push({ phrase: this.stringArray[i], wordCatagory: "command" });
}
return;
// find items words
}, this);
// find items
Game.items.forEach(function(item) {
if (jWord === item.name) {
// find two word items
this.seperateStringArrays.push( { phrase: this.stringArray[i] + " " + this.stringArray[i + 1], wordCatagory: "item" });
// Remove word that has been joined with previous word
this.stringArray.splice(i + 1, 1);
return;
// find single word items
} else if (this.stringArray[i] === item.name) {
this.seperateStringArrays.push({ phrase: this.stringArray[i], wordCatagory: "item" });
}
return;
}, this);
// Add uncatagorised
this.seperateStringArrays.push({ phrase: this.stringArray[i], wordCatagory: "uncatagorized" });
};
// DOn't forget to empty string array after new arrays are created
console.log(this.seperateStringArrays);
// sort sentences into seperate commands
this.seperateStringArrays = [];
}
}
// catagorise word
// loop through string array
// check if word or pair of words match a command
// If so, end iteration and start next
// Check if word or word pair match an item
// If so, end iteration and start next iteration
【问题讨论】:
标签: javascript for-loop iteration