-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathAlphabetical Sequence
20 lines (17 loc) · 1018 Bytes
/
Alphabetical Sequence
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
Description:
In this kata you will be given a random string of letters and tasked with returning them as a string of comma-separated sequences sorted alphabetcally, with each sequence starting with an uppercase character followed by n-1 lowercase characters, where n is the letter's alphabet position 1-26.
Example
alphaSeq("ZpglnRxqenU") -> "Eeeee,Ggggggg,Llllllllllll,Nnnnnnnnnnnnnn,Nnnnnnnnnnnnnn,Pppppppppppppppp,Qqqqqqqqqqqqqqqqq,Rrrrrrrrrrrrrrrrrr,Uuuuuuuuuuuuuuuuuuuuu,Xxxxxxxxxxxxxxxxxxxxxxxx,Zzzzzzzzzzzzzzzzzzzzzzzzzz"
Technical Details
The string will include only letters.
The first letter of each sequence is uppercase followed by n-1 lowercase.
Each sequence is seperated with a comma.
Return value needs to be a string.
*/
function alphaSeq (str) {
let sorted=str.toLowerCase().split('').sort();
let repeating=sorted.map(v=>v.charCodeAt()-96);
return sorted.map((v,i)=>v.repeat(repeating[i]).slice(0,1).toUpperCase()+
v.repeat(repeating[i]).slice(1)).join(',');
}