Javascript String lowerLetters(value)

Description

Javascript String lowerLetters(value)



/**/*from w ww .java 2s  .co  m*/
 * Takes a string with any characters.
 * Returns an object containing appearence of every distinct letters in lower case.
 *
 * @param {string} value
 * @return {Object}
 *
 * @example
 *  'Who you are, Buddy?' => { a:1, d:2, e:1, h:1, o:2, r:1, u:2, y:2 }
 *
 */

function lowerLetters(value) {
  return value.match(/([a-z])/g).reduce((acc, char) => {
    if(acc[char]) {
      acc[char]++;
      return acc;
    } else {
      return Object.assign(acc, { [char]: 1 });
    }
  }, {});
}



PreviousNext

Related