View Full Version : I am given an error when checking equality between an array variable and a letter.

12-04-2002, 09:33 AM
About half a year ago, I used to program in JScript but stopped as I tried to learn C++. I'm trying to make a rather simple script actually, and am almost done other than one problem. I have a value in an array variable, and try to check it's equality with a letter, for instance, the letter a. Here is the line of script giving me the problem:


Say that b equals 0, and the first variable in the array (codeArray[0]) has the value of the letter a. This line should check to see if the array variable is equal to the letter a, which is true. However, I know this isn't right and that you have to have a special command to do this. Do any of you know the command I need? And please don't tell me to change the " to ' because I tryed that (hey, it works in Java some times). Any help is appreciated.

12-04-2002, 09:54 AM
I can't see anything wrong with that code. What problem are you encountering?

12-04-2002, 05:51 PM
I think you mean this..

if(codeArray[b].indexOf("a")!= -1){

12-04-2002, 06:16 PM
Post more of your script, you must be missing something somewhere else. As you can see form this example it works out:

var codeArray=["a","b","c"];
var b=0;
alert("its working");

12-04-2002, 09:32 PM
well, he says he wants to check for the 'existance' of a certain letter within a array value...

test this

var codeArray=["rush","scorpions","iron maiden"];
for(x=0; x<codeArray.length ; x++){
if(codeArray[x].indexOf("a")! = -1){

alert(codeArray[x] +"has the letter a");

}else{alert(codeArray[x] +"NO letter a")


it just goes thru each value in the array, and checks to see if the letter "a" is in each of those values..
rush doesn't
scorpions doesn't
iron maiden does

12-04-2002, 10:56 PM
Like I said we need to see more of his code and need more clarification. In the original statement he says:

This line should check to see if the array variable is equal to the letter a, which is true.

Which is what my example shows. If in-fact he is simply trying to see if the value contains an "a" at all rather than equaling "a" then yes BrightNail offers a viable solution.

Hellfire, please clarify so we can assist you further.

12-05-2002, 12:10 AM
*slams his head on his desk* I don't know why I didn't think of using that, when I have used it several times before. But I know there is a special command for what I'm trying to do. *shrug* Oh well, this should work. Thanks, but now I feel stupid... lol