Jump to content

Splice() [Undefined] Is Not A Function


Costanza

Recommended Posts

I have some code that was working fine until I added one extra line to one of my functions. The added line:

currentOptions = removeIncompatibleOptions(currentOptions); 

It keeps giving me this error when I call the function that has that line in it: Result of expression 'currentOptions.splice' [undefined] is not a function. Is currentOptions not actually an array? That's the only reason I can think of for why it would say that splice() was undefined. It is declared globally like this:var currentOptions = new Array(); This is the removeIncompatibleOptions() function that has splice() in it:

function removeIncompatibleOptions(currentOptions){if (currentCategory == 'handguard'){var compType;if ($('barrel').hasClassName('comp228')){compType = 'comp228';}else if ($('barrel').hasClassName('comp318')){compType = 'comp318';}else if ($('barrel').hasClassName('comp400')){compType = 'comp400';}else if ($('barrel').hasClassName('comp480')){compType = 'comp480';}for (var i = 0; i < currentOptions.length; i++){if (!currentOptions[i].hasClassName(compType)){currentOptions.splice(i, 1);}}}}

Link to comment
Share on other sites

the problem might be looping through the array and removing one from it each time, which might leave you with an empty array that you can't splice from anymore. First thing it to learn to debug properly. Verify the value of currentOptions as it's being passed into the function. Then it could never hurt to log for each iteration.

console.log(currentOptions);for(var i = 0; i < currentOptions.length; i < l){  console.log('i is => ' + i);  console.log(currentOptions);  //conditional code  if(){ ... }  console.log(currentOptions);};

figure out the state of currentOptions at the start and end of each iteration so you can know for sure what exactly is going on.

Link to comment
Share on other sites

I recall from your previous postings that you are assigning a value to currentOptions, but I don't remember what. Post the code that assigns the values to currentOptions. Also, what line of your code does the error point to?
This line assigns the values to currentOptions:
currentOptions = document.getElementsByClassName(currentCategory);

The error points to the last line of the code I posted inmy OP. This one:

currentOptions[color=#676600].[/color]splice[color=#676600]([/color]i[color=#676600],[/color] [color=#066667]1[/color][color=#676600]);[/color]

I checked the array length in the console (after running the function) and it is full length. in other words, removeIncompatibleOptions did nothing, weirdly.

Link to comment
Share on other sites

ah, that's why. getElementsByClass name returns a node list, which is not the same as a native array.https://developer.mozilla.org/en/DOM/document.getElementsByClassNamehttps://developer.mozilla.org/en/DOM/NodeList

Link to comment
Share on other sites

ah, that's why. getElementsByClass name returns a node list, which is not the same as a native array.https://developer.mo...entsByClassNamehttps://developer.mo...en/DOM/NodeList
Ok cool, so I should be declaring a nodeList instead of an Array, and be using nodeList methods instead of splice()? or is there an equivalent method to getElementsByClassName that returns an array? Or I guess I could do an extra loop first to put the nodeList items into a new array one by one? NodeList doesn't seem to have many methods for altering it.
Link to comment
Share on other sites

Cooooo-stanza! (can't-stand-ya... :P)
Oh yeah? Well the jerk store called and they're running out of YOU! :D Seriously though, I appreciate it. When my little project here is done hopefully I can link it so you guys can see what you've been helping with. It's basically an interface for customizing assault rifles with real parts, similar to what some games do but much more in-depth.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...