PDA

View Full Version : Javascript concat problem



Jen333
09-08-2008, 12:08 PM
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!!:confused:

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!! :)

Philip M
09-08-2008, 02:29 PM
<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?

Jen333
09-08-2008, 02:47 PM
Thank you!! I will try this out. :thumbsup: 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!

rnd me
09-09-2008, 05:52 AM
there is a bug in your and phillip's code.

try:


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.

Jen333
09-09-2008, 08:00 AM
Just to let you know this solved my problem, so thank you!! :)

Philip M
09-09-2008, 08:22 AM
there is a bug in your and phillip's code.

try:


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? :D

Yes, my bad - I did not notice alphabetArray

Revised script:-



<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>

rnd me
09-10-2008, 02:20 AM
EDIT: nevermind, i was gonna make a joke, but decided not to...

Philip M
09-10-2008, 07:21 AM
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.