5 Replies Latest reply on Dec 10, 2009 8:57 AM by MarionMacDonald

    charAt not working

    MarionMacDonald

      I'm working on a word game but not getting expected results from charAt.

       

      I've got an array of words, one of which is picked at random.   I then check to see if the letter clicked is present in the word.

       

      This is part of my function:

       

      function checkCorrect(letter){
      for (i=0; i<_root.whichWord.length; i++) {
        trace(letter);                                                          //this traces correctly
        trace(_root.whichWord);                                         //this traces correctly  
        trace(_root.whichWord.charAt[i]);                            //this gives 'undefined'

       

      Could it be something to do with whichWord being an Array element - although it is a string?

       

      Puzzled.

        • 1. Re: charAt not working
          Ned Murphy Adobe Community Professional & MVP

          It's hard to tell what's going on in that code without having the whole picture of what's involved with it.  But if you wnt to see if a letter is in a word, you can just use the String.indexOf() method to determine if it exists in the string or not.

          • 2. Re: charAt not working
            MarionMacDonald Level 1

            Thanks for your reply.  I had been trying indexOf also - with the same results.  Here is my code so far:

             

            arrWords=["acràs","pàthadh","briògais","ceàrt","ceàrr","peàta","deòch","àireamhair","airgè ad","anmòch","càfaidh","eàglais","garaìds","oifís","ospàdal","sgòil","stèisean","bùth","cl às","màdainn","tàlla","bùidhe","dùbh","uàine","dònn","gèal","dèarg","gòrm","àrd","mòr"]
            arrLetters=["a","à","b","c","d","e","è","f","g","h","i","ì","l","m","n","o","ò","p","r","s ","t","u","ù"];

            //display first line of letter buttons
            for (i=0; i<12; i++) {
            var letterBtn = attachMovie("guessLetter", "guess" + i, 100+i, {_x: i*25, _y: 320});
            _root["guess"+i].displayLetter=arrLetters[i];
            this.letterBtn.onRelease = function() {
              var letterClicked = eval(this._name).displayLetter;
              checkCorrect(letterClicked);
            }
            }
            //display second line of letter buttons
            for (i=12; i<arrLetters.length; i++) {
            var letterBtn = attachMovie("guessLetter", "guess" + i, 100+i, {_x: (i-12)*25, _y: 360});
            _root["guess"+i].displayLetter=arrLetters[i];
            this.letterBtn.onRelease = function() {
              letterClicked = eval(this._name).displayLetter;
              checkCorrect(letterClicked);
            }
            }
            //pull random word from array
            var whichWord = arrWords[Math.floor(Math.random() * arrWords.length)];
            trace(whichWord);

            //display blanks for word
            for (i=0; i<whichWord.length; i++) {
              var thisX = 50;
              attachMovie("blankLetter", "blank" + i, 200+i, {_x: thisX+(i*25), _y: 50});
            }

            function checkCorrect(letter){
            for (i=0; i<_root.whichWord.length; i++) {
              trace(letter);
              trace(_root.whichWord);
              trace(_root.whichWord.charAt[i]);
              if (letter == _root.whichWord.charAt[i]) {
               _root["blank"+i].displayLetter = letter;
               _root["guess"+i]._visible=false;
              }
            }
            }

             

            I have accented characters embedded in the textboxes.

            • 3. Re: charAt not working
              Ned Murphy Adobe Community Professional & MVP

              In response to your problem with using charAt, it is a function, not any array.

               

              use charAt(i) instead of charAt[i]

              • 4. Re: charAt not working
                MarionMacDonald Level 1

                Oh thank you.  Of course it is.

                 

                Signed:

                Dummy

                • 5. Re: charAt not working
                  Ned Murphy Adobe Community Professional & MVP

                  You're welcome...

                   

                  Signed

                  Fellow Dummy