0

I have an array of characters:

'1','2','3','4','5','6','7','8','9'

That I need to transform into:

'(1)','(2)','(3)','(4)','(5)','(6)','(7)','(8)','(9)'

I recall there is a function to do so in coffeescript but cannot locate where or how it was defined.

How can I efficiently append opening and closing parenthesis to each object in the array?

5
  • Use simple loop or map function for functional programming look. Commented Mar 28, 2015 at 0:52
  • 1
    It's not clear what you're trying to do, especially with this wierd syntax to define arrays. Are you truing to add '(' and ')' character to the beginning and to the end of each string in an array? Commented Mar 28, 2015 at 0:54
  • @LeonidBeschastny Yes, Commented Mar 28, 2015 at 0:56
  • Don't forget you can express your array as 1..9, assuming that's your actual code. Commented Mar 30, 2015 at 9:25
  • @Cheezmeister I'm sorry, it's not, I just used that for an example Commented Mar 30, 2015 at 13:13

1 Answer 1

1

Just as Alexander Ravikovich mentioned in comments, you could do it using either a loop:

arr = ['1', '2', '3']

new_arr = ("(#{s})" for s in arr)
# => [ '(1)', '(2)', '(3)' ]

or an Array::map function:

new_arr = arr.map (s) -> "(#{s})"
# => [ '(1)', '(2)', '(3)' ]

Note that I used CS string Interpolation to simplify my code.

Sign up to request clarification or add additional context in comments.

2 Comments

Where can I test this in the browser? Like js fiddle but for coffeescript
By the way, jsfiddle supports coffeescript as well.

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.