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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Why does this script return two commas?

    This script is supposed to convert two numeric variables to strings, store the characters of those strings into arrays and show an alert with the array values, but the alert only contains commas, so nothing was stored into the arrays, and I don't understand why. Any help is much appreaciated.

    The variables sum1 and sum2 contain a numeric value (yes, I've tested that), and the variables numarray1 and numarray2 are empty arrays before this code.

    Code:
        //convert numeric sums to strings
       sum1str = sum1 + "";
       sum2str = sum2 + "";
    
    
        //stores digits of sums in arrays
       for(i = 0 ; i < sum1str.length ; i++)
        {
         numarray1[i] = sum1str.charAt[i];
        }
    
       for(i = 0 ; i < sum2str.length ; i++)
        {
         numarray2[i] = sum2str.charAt[i];
        }
    
       alert(numarray1 + "     " + numarray2);

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    1)make sure you have declared your arrays and there lengths.
    2)use a different variable other than i for the second loop, use k or any other letter
    3)change your square brackets in the part
    numarray[i] = sumstr.charAt[i];

    to round brackets so it becomes:

    numarray(i) = sumstr.charAt(i);


    and that should fix it.

    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks. All that wasn't neccessary, I just changed the brackets for charAt.


  •  

    Posting Permissions

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