28

Here is the code:

function accum(s) {
  return s.split('').map((x,index) => x.toUpperCase()+Array(index+1).join(x.toLowerCase())).join('-');
}

I would like to know what "=>" is. This function takes a string and for the index number of each element it adds that many elements to the output. Here's an example:

accum("abcd") --> "A-Bb-Ccc-Dddd"
accum("RqaEzty") --> "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
accum("cwAt") --> "C-Ww-Aaa-Tttt"
MortiestMorty
  • 505
  • 1
  • 4
  • 12
  • 3
    This is a new ES6 feature called "Arrow Functions". See; https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – Rocket Hazmat Jul 15 '16 at 21:40
  • 1
    Better duplicate: http://stackoverflow.com/questions/24900875/whats-the-meaning-of-an-arrow-formed-from-equals-greater-than-in-javas – JJJ Jul 15 '16 at 21:41
  • Yes, I found the other first – mplungjan Jul 15 '16 at 21:44
  • 1
    So can we have the link at the top be to the question @JJJ found? This question is "What is..." The supposed duplicate question linked above is "When do I..." Not the same thing at all. – jaredad7 Aug 23 '18 at 17:15

1 Answers1

23

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.

So in your case

s.split('')

splits the input on empty spaces and for each element of the resulted array you apply the following function:

(x,index) => x.toUpperCase()+Array(index+1).join(x.toLowerCase())

The left part is the random element, x of the array (s.split('')) and it's corresponding index. The second part applies a transformation to this input.

Nicholas K
  • 14,118
  • 7
  • 25
  • 49
Christos
  • 50,311
  • 8
  • 62
  • 97
  • Not the downvoter, but I'm going to guess it's because this a dupe of a well-known question and due to this answer it won't be deleted by the [Roomba](https://stackoverflow.com/help/roomba). – Kyll Jul 15 '16 at 21:48
  • so annoying... I'd be more than happy to correct my issue if someone would just tell me. Anyways, thanks for answering my question. One more question, what is "Array()"... I can't find this method anywhere. – MortiestMorty Jul 15 '16 at 21:49
  • What I don't understand is the "Array(index+1)... I get what it's doing, but what is "Array()"? I can't find this syntax/method/rule anywhere – MortiestMorty Jul 15 '16 at 21:55
  • The `Array(index+1)` creates an array of length `index+1`. – Christos Jul 15 '16 at 21:58
  • I understand this part, but I don't understand this syntax. a capitalized method? If i make the method "array(index+1)" instead of "Array(index+1)" it doesn't work. Why? What kind of syntax is this? I've never seen anything like this and I can't find it in the new ES6 documentation regarding arrow functions. – MortiestMorty Jul 15 '16 at 22:03
  • 1
    This is irrelevant with ES6. In order you get it, open developers tools and in the console write this, `var arr = Array(4)`. Then write, `arr.length`. As you guess you will get 4. Why? Because you have created an array of length 4. What are the values in the `arr`? Undefined ! You could check it fairly easy, if you write `arr[0]`, `arr[1]`,... – Christos Jul 15 '16 at 22:08
  • Have a look here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array under Syntax. – Christos Jul 15 '16 at 22:10
  • I have never seen this, but this is very helpful. Thank you so much! I'd give you a million upvotes if I could. – MortiestMorty Jul 15 '16 at 22:11
  • @Twigs haha :) You are very welcome. I am glad that I helped ! – Christos Jul 15 '16 at 22:11