I'm facing a challenge with this particular code snippet. Essentially, I am attempting to loop through an array of Word objects and categorize them based on word type using a switch statement. This process is triggered by jQuery listening for a button click.
for (var i=0; i < wordList.length; i++)
{
switch (wordList[i].type) {
case "1": nouns.push(wordList[i].word); break;
//"1" represents a noun type, and the "word" property contains the actual word string
... //Additional cases for other word types
}
}
Initially, the words were not being added to the nouns array. So, I modified the line under case "1" as follows:
case "1": nouns.push(wordList[i].word); asdf = nouns; asdf2 = wordList[i].word; break;
By omitting 'var', asdf and asdf2 unintentionally became global variables, allowing me to interact with them in the console:
asdf
asdf2
Displayed [] and "I", respectively, showing that the word was successfully retrieved, but not appended to the array.
asdf.push(asdf2)
Resulted in 1, and upon further inspection of asdf, it showed ["I"].
Any insights on what might be causing this issue?
Edit: Including full relevant code snippet
//Declare arrays
var articles=[], properNouns=[], nouns=[], pluralNouns=[], adjectives=[], conjunctions=[], verbs=[], pluralVerbs=[], adverbs=[], prepositions=[], interrogatives=[];
//Organize words into respective arrays
for (var i=0; i < wordList.length; i++)
{
switch (wordList[i].type) {
case "1": nouns.push(wordList[i].word); asdf = nouns; asdf2 = wordList[i].word; break;
case "11": pluralNouns.push(wordList[i].word); break;
case "12": properNouns.push(wordList[i].word); break;
case "2": verbs.push(wordList[i].word); break;
case "21": pluralVerbs.push(wordList[i].word); break;
case "3": adjectives.push(wordList[i].word); break;
case "4": adverbs.push(wordList[i].word); break;
case "5": conjunctions.push(wordList[i].word); break;
case "6": prepositions.push(wordList[i].word); break;
case "7": interrogatives.push(wordList[i].word); break;
case "8": articles.push(wordList[i].word); break;
default: console.log("Error, could not sort "+wordList[i].word); break;
}
}