Aakash Doshi
Aakash Doshi

Reputation: 202

Based on array length create a concat string using javascript

I have

array.length=3
array2=['[A]','[B]','[C]','[D]'];

based on array.length, i want to create set of string
example, if lenght=3
output= [A][B][C]

example, if lenght=2
output= [A][B]

i tried using for loop but it prints vertically and could not store and append ,I have no idea how to print horizontally,any ideas or help would be really appreciated
my output is

[A]
[B]
[C]
AND Then i store it in variable and tried appending them. I know this is not the best idea

Upvotes: 0

Views: 1062

Answers (1)

Yury Tarabanko
Yury Tarabanko

Reputation: 45121

Try

console.log(array2.slice(0, array.length).join(''));

Though I don't see any point in having the first array. You could use just var len = 3;.

Upvotes: 1

Related Questions