Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2002
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I am given an error when checking equality between an array variable and a letter.

    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:

    if(codeArray[b]=="a"){

    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.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,965
    Thanks
    0
    Thanked 236 Times in 233 Posts
    I can't see anything wrong with that code. What problem are you encountering?

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I think you mean this..

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

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    Post more of your script, you must be missing something somewhere else. As you can see form this example it works out:

    PHP Code:
    var codeArray=["a","b","c"];
    var 
    b=0;
    if(
    codeArray[b]=="a"){
      
    alert("its working");


  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.

  • #7
    New Coder
    Join Date
    Dec 2002
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    *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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •