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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript concat problem

    Hi! I am working on a project and have an issue with the concat method i am using on 2 arrays. I get a string input from a user:
    'abcde' and assign this to var alphabet
    then this is the code I am using:

    var alphabetArray = alphabet.split(''); //turn input string into array
    var extraLettersArray = ['x', 'y', 'z']; //create array with extra letters
    var completeArray = alphabet.concat(extraLettersArray); //join arrays
    var completeString = completeArray.join(); // output as a string

    the problem is that when i turn the alphabet input string into an array, i get
    a,b,c,d,e,
    i notice there is a comma at the end of the e here, but when i create the other array, there is not comma at the end..i think this is then causing the problem below when i concat these arrays.
    x,y,z

    when i concat the arrays i get:

    a,b,c,d,e,,x,y,z
    and then when i use .join i get:
    abcde xyz
    with a gap!

    if i concat the other way round, ie. with xyz at the beginning i don't get this problem..
    x,y,z,a,b,c,d,e and when i make this a string with .join it is fine i get:
    xyzabcde

    but i just can't get this to work the way round i need it to!
    i have tried the inital .split() as just .split and tried to join the xyz into a string with .join before the concat but it still doesn't work as then i get:
    abcde
    xyz
    and after concat and join:
    abcde xyz

    so it gives me a gap here!!

    This is driving me nuts! i want to use the concat, split and join methods on arrays here, but can anyone see what might be causing this issue and how i can get rid of it?

    Thank you so much!!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Code:
    <script type = "text/javascript">
    
    var alphabet = "abcdefgh";
    var alphabetArray = alphabet.split(''); //turn input string into array
    var extraLettersArray = ['x', 'y', 'z']; //create array with extra letters
    var completeArray = alphabet.concat(extraLettersArray); //join arrays
    completeArray = completeArray.replace(/[\s\,]/g,"");  // strip commas and spaces
    
    alert (completeArray);  // a string
    
    </script>
    But why not simply:-

    var a = "abcdefg";
    var b = "xyz";
    var c = a+b;


    Quizmaster: Paris is bigger than Vienna. And Vienna is bigger than Rome. Which is the smallest?
    Contestant: Oh, I dunno. Paris?
    Last edited by Philip M; 09-08-2008 at 03:41 PM.

  • #3
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you!! I will try this out. The reason for using the arrays concat method is just that I am working on this for a exercise and wanted to practice with these methods, but your adding of the variables is certainly the easiest way! thank you again!

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,460
    Thanks
    11
    Thanked 600 Times in 580 Posts
    there is a bug in your and phillip's code.

    try:
    Code:
    var alphabetArray = alphabet.split(''); //turn input string into array
    var extraLettersArray = ['x', 'y', 'z']; //create array with extra letters
    var completeArray = alphabetArray.concat(extraLettersArray);
    you also cannot use completeArray.replace

    replace works only for stings, not arrays.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #5
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just to let you know this solved my problem, so thank you!!

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by rnd me View Post
    there is a bug in your and phillip's code.

    try:
    Code:
    var alphabetArray = alphabet.split(''); //turn input string into array
    var extraLettersArray = ['x', 'y', 'z']; //create array with extra letters
    var completeArray = alphabetArray.concat(extraLettersArray);
    you also cannot use completeArray.replace

    replace works only for stings, not arrays.
    Would that be a sting(ar)ray?

    Yes, my bad - I did not notice alphabetArray

    Revised script:-


    Code:
    <script type = "text/javascript">
    
    var alphabet = "abcdefgh";
    var alphabetArray = alphabet.split(''); //turn input string into array
    var extraLettersArray = ['x', 'y', 'z']; //create array with extra letters
    var completeArray = alphabetArray.concat(extraLettersArray); //join arrays
    var ca = completeArray.join("")
    alert (ca);  // a string
    
    </script>
    Last edited by Philip M; 09-09-2008 at 09:36 AM.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,460
    Thanks
    11
    Thanked 600 Times in 580 Posts
    EDIT: nevermind, i was gonna make a joke, but decided not to...
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by rnd me View Post
    EDIT: nevermind, i was gonna make a joke, but decided not to...
    Well, if you do, please type it very slowly as my brain is not too quick on the uptake this morning.


  •  

    Posting Permissions

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