JavaScript function to automatically count consecutive letters in a string

arrays, concatenation, counter, javascript, string

Solution

You can use a regular expression to match any letter followed by zero or more instances of the same letter.

`rx=/([a-zA-Z])\1*/g;`

Your example matches `["eeeee","oooo","hh","ooo","ee"]`.

Using map, return the initial letter and the number of occurrences in a new array for each index.

function letterCount(str) {
  var s = str.match(/([a-zA-Z])\1*/g) || [];
  return s.map(function(itm) {
    return [itm.charAt(0), itm.length];
  });
}

console.log(letterCount("eeeeeoooohhoooee"))

returned value: (Array)

[["e",5],["o",4],["h",2],["o",3],["e",2]]

NOTES:

- var s= str.match(/([a-zA-Z])\1/g)||[];*

returns an array of matches (repeated letters) or an empty array([]). Otherwise, if the string does not contain any letters an error would be thrown (from calling map on null).

\1* is used to allow matching instances of a single letter with any or no sequential repetition. '\1+' would not match a single unrepeated letter.

Array map expects a function and passes three arguments- the value at each index, the index number, and a reference to the entire array. In this case, only the value of each index is used, so we can ignore the other arguments.

Problem

I am attempting (unsuccessfully) to write JavaScript function LetterCount to count the consecutive letters in a string (and not the total number). Ideally: LetterCount("eeeeeoooohhoooee") = [["e", 5],["o",3],["h",2],["o",3],["e",2]] The following code attempts to count the number of consecutive letters in a string only when I already know what they are: ``` function LetterCount(str) { for (var i=0; i<str.length;i++) { var arr1=[]; arr2=[]; arr3=[]; arr4=[]; arr5=[]; var e=0; o=0; h=0; o2=0; e2=0; if(str[i]="e") { arr1 += "e"; e++; } arr1.push(e); if(str[i]="o") { arr2 += "o"; o++; } arr2.push(o); if(str[i]="h") { arr3 += "h"; h++; } arr3.push(h); if(str[i]="o") { arr4 += "o"; o2++; } arr4.push(o2); if(str[i]="e") { arr5 += "e"; e2++; } arr5.push(e2); } return arr1.concat(arr2).concat(arr3).concat(arr4).concat(arr5); } ``` In the code above, I need to first know what the letters in the string are, and how many of them are present, in what order. INSTEAD: How do you write a function that will automatically recognize the letter themselves, and then return the count of consecutive letters. Would also be great if the answer is in the following format: ``` LetterCount("eeeeeoooohhoooee") = [["e", 5],["o",3],["h",2],["o",3],["e",2]] ``` Any help is much appreciated!

Original source