5 Replies Latest reply on Dec 6, 2008 4:51 AM by Ned Murphy

    NaN Grrrr

      I have an array of numbers that I want to randomize but... after I randomize the orders of the numbers they are no longer act like numbers.

      So basically I have cardArray which contains 16 integers. I use a randomized function (code attached) to reorder the array into a new array so I can have a dynamic list of numbers but if I take any of the numbers out of the array and try to use the in a mathematical statement I get NaN. Its like the randomization process is converting them to something other than numbers.

      Help greatfully appreciated

        • 1. Re: NaN Grrrr
          kglad Adobe Community Professional & MVP
          cardArray.length is probably undefined.
          • 2. Re: NaN Grrrr
            rshaedyn Level 1
            Hi Kglad:

            Thanks for replying to my question - much appreciated!

            Unfortunately, I am still fighting the NaN - i put in trace statements to make sure that the cardArray.length was being tracked and I can see in the output panel as it is gradually being spliced down to nothing while the randArray is being filled. If cardArray.length was undefined wouldn't that result in randArray being empty?

            At the end of the randomization process I do end up with randArray filled with numbers in a different order so I was able to "sucessfully" transfer them from the cardArray to the new randArray. And I can look at say randArray[1] which shows, for example, 5 so it looks like I have everything I need except that if I try to do any math such as
            trace(randArray[1] +1) I get the dreaded NaN instead of 6.I

            The cardArray contained numbers and math functions worked on the contents at the various indexed locations. After randomization, I end up with a new array with what should be the same contents (in a different order) and it looks right when I do a trace statement but... again any attempt to work math results in the NaN.

            Any other suggestions?
            Again thanks for taking the time out of your day to point towards a possible solution.


            • 3. NaN Grrrr
              Ned Murphy Adobe Community Professional & MVP
              Geez Leslie, this was a stumper until I tried something that worked and then went to check in the splice method to see what it returns. Therein lies the problem... it returns an array, which makes sense, not the value you removed. So when you trace it at the end, you are tracing an array of arrays, each holding just one value. If you had used randArray[#][0] you would have gotten the numbers you were looking for.

              Change it to:

              randArray[ i ] = cardArray[randNumber];
              var dontCare = cardArray.splice(randNumber, 1);

              and your problems should be over.

              It will work without the "var dontCare =", but I don't know where in the ethereal it ends up otherwise.
              • 4. Re: NaN Grrrr
                rshaedyn Level 1
                Hi Ted:

                Thank you, Thank you, Thank you!!!!!
                You just became my favourite super hero - this thing had me pulling my hair out.

                And... after all the debugging and things I tried it not once occurred to me that the splice was the problem and to look at the documentation for it. Major blind spot!!!!

                Again - thanks so much!
                • 5. NaN Grrrr
                  Ned Murphy Adobe Community Professional & MVP
                  You're welcome.... if I were a super hero I would have spotted it right away with my amazing been there done that vision--but it took awhile instead. I have done this same routine quite a few times and probably did the hair pulling bit the first time around because after that it just became second nature to me to do it as I showed you, apparently forgetting why.