DEV Community

Cover image for Day 19 of JavaScriptmas - Alphabet Subsequence Solution
Sekti Wicaksono
Sekti Wicaksono

Posted on • Updated on

Day 19 of JavaScriptmas - Alphabet Subsequence Solution

Day 19 challenge is checking a letter duplication in a string.
To solve this a string also must be sorted. If there are some characters duplication in a single string return false otherwise return true

For example,
A string effg will return false because there are 2 letter f.
But the string ace will return true since there is no letter duplication.

There is the JavaScript Solution

function alphabetSubsequence(str) {
    // If letters NOT in ascending order and detect duplication return false  
    let result = str.split('').map( (val, index) => (str.charCodeAt(index) >= str.charCodeAt(index+1)) ? false : true);

    // if result for ALL value are TRUE, return true, otherwise return false
    return result.every( val => val == true);    
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)