0

I have an array of string:

public scene: Array<string> = ['gittare','saxsophone','drum'];

I need to concatenate all the string in the array like 'gittare_saxsophone_drum' . Here is my solution in recursive way:

addressCreator(array:Array<string>){


      if(array.length>0)
           var fileName=array[0]+"_"+this.addressCreator(array); 
      else
          return [];
      return fileName;
  }

The none recursive approach could be:

addressCreator(array:Array<string>){

      array.splice(0,1);
      for(let e of array)
           var fileName=e+"_"+fileName; 

      return fileName;
  }

In both of these solutions, i nused var which shouldn't be used in functional programming.

So what is the best approach?

2
  • 1
    I cannot see why you are using splice, as splice removes an element from your parameter. This is more of a problem in functional programming as this makes your function a non pure one. After you called your function, your array will be empty and that could have quite some repercussions in other parts of your code Commented Dec 18, 2016 at 12:57
  • I suggest you read through the documentation pages on arrays so you know what features and methods are available to you. Commented Dec 18, 2016 at 15:56

1 Answer 1

3

You can just use the join method for that.

let x = scene.join("_");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.