I've been pletely stumped by this bit of code here. Basically, I'm trying to go through an array of Word objects, and organize them based on word type using a switch statement. This is all triggered by jQuery waiting for a button to be pressed.
for (var i=0; i<wordList.length; i++)
{
switch (wordList[i].type) {
case "1": nouns.push(wordList[i].word); break;
//"1" is the type flag for noun, the "word" property is the string containing the word
... //Rest of word types
}
}
The word wouldn't actually get assigned to the nouns array. So I changed the case "1" line to this:
case "1": nouns.push(wordList[i].word); asdf = nouns; asdf2 = wordList[i].word; break;
Without var, asdf and asdf2 became implicitly global, so I could play with them in console:
asdf
asdf2
returned [] and "I" respectively, so it could pick up the word, but it didn't add it to the array.
asdf.push(asdf2)
Returned 1 and the next log of asdf gave me ["I"].
What's wrong here?
Edit: Full relevant code
//Declare arrays
var articles=[], properNouns=[], nouns=[], pluralNouns=[], adjectives=[], conjunctions=[], verbs=[], pluralVerbs=[], adverbs=[], prepositions=[], interrogatives=[];
//Sort words into proper 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;
}
}
I've been pletely stumped by this bit of code here. Basically, I'm trying to go through an array of Word objects, and organize them based on word type using a switch statement. This is all triggered by jQuery waiting for a button to be pressed.
for (var i=0; i<wordList.length; i++)
{
switch (wordList[i].type) {
case "1": nouns.push(wordList[i].word); break;
//"1" is the type flag for noun, the "word" property is the string containing the word
... //Rest of word types
}
}
The word wouldn't actually get assigned to the nouns array. So I changed the case "1" line to this:
case "1": nouns.push(wordList[i].word); asdf = nouns; asdf2 = wordList[i].word; break;
Without var, asdf and asdf2 became implicitly global, so I could play with them in console:
asdf
asdf2
returned [] and "I" respectively, so it could pick up the word, but it didn't add it to the array.
asdf.push(asdf2)
Returned 1 and the next log of asdf gave me ["I"].
What's wrong here?
Edit: Full relevant code
//Declare arrays
var articles=[], properNouns=[], nouns=[], pluralNouns=[], adjectives=[], conjunctions=[], verbs=[], pluralVerbs=[], adverbs=[], prepositions=[], interrogatives=[];
//Sort words into proper 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;
}
}
Share
Improve this question
edited Jun 14, 2013 at 1:40
Rstevoa
asked Jun 14, 2013 at 1:22
RstevoaRstevoa
2714 silver badges17 bronze badges
5
- 1 how is nouns declared? – karthikr Commented Jun 14, 2013 at 1:23
- 1 yes, please show full code... – shennan Commented Jun 14, 2013 at 1:27
- I have edited to include all the code I think could be relevant. If you would like more, please let me know – Rstevoa Commented Jun 14, 2013 at 1:41
- 1 Show us your wordList declaration, please. – Walter Macambira Commented Jun 14, 2013 at 1:46
- I unfortunately believe I will have to close/delete this question, because I don't think I can find a satisfactory explanation. It turns out the issue was caused somewhere entirely different in the filesystem which didn't even contain any script, and I suspect that it has to do with a subversion bug of some sort. If I can find the exact cause, and it has potential to help someone else with a similar issue, I'll post it. Otherwise I'm taking this down. Thanks for your assistance, everyone. – Rstevoa Commented Jun 14, 2013 at 2:36
3 Answers
Reset to default 1here is a JSFiddle example.
The only changes made from your code to the example:
definition of wordList
in jsfiddle example a div tag to append output to
It appears to do what you want. Is your definition of wordList different?
$(document).ready(function () {
//Declare arrays
var articles = [], properNouns = [], nouns = [], pluralNouns = [], adjectives = [], conjunctions = [], verbs = [], pluralVerbs = [], adverbs = [], prepositions = [], interrogatives = [];
var wordList = [{ 'type': "1", 'word': 'foo' },
{ 'type': "1", 'word': 'foo1' },
{ 'type': "1", 'word': 'foo2'},
{ 'type': "1", 'word': 'foo3' }];
//Sort words into proper 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;
}
}
for (var i in nouns) {
console.log(nouns[i]);
$('#output').append(nouns[i] + '<br>');
}
console.log(nouns);
});
Cause of the problem: A data file had been modified and the VERBS were no longer tagged correctly, leading to no sentences being generated
Why the array was empty:
The system used to process the arrays emptied them, and the new variable asdf
merely pointed to the array, which was empty by the time I used it in console
How I could have avoided a lot of this:
case "1": nouns.push(wordList[i].word); asdf = nouns.slice(0); break;
Let this be a Public Service Announcement. Copy your array during debug.
Yep - digging up an old thread...
I had this same problem today. Took me a while to figure out that I had a Watch Expression in Chrome Debugger that emptied the array..
so code to push looked like this:
$scope.emailMsg.Bcc.push(EmailAddress);
Watch expression in debugger like this
$scope.emailMsg.Bcc = [];
So - every time I stepped my array would zero out :)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745416086a4626754.html