Home > Software engineering >  Count all characters in string and output as string in ES6 Javascript
Count all characters in string and output as string in ES6 Javascript

Time:07-27

Javascript implementation to print the characters and frequencies in the order of their occurrence. Please if anyone knows a better solution how to count characters in the string and output the result as a string.

See test cases below

function process (str) {
  
  //reject empty string
  if (!str) return "";
  
  //clean up
   str = str.replace(/[\s\W\d]/g, '').toLowerCase();
  
  //get result
  let result = str.split('').reduce((total, letter) => {
    total[letter] ? total[letter]   : total[letter] = 1;
    return total;
  }, {});
  
   //convert to string
   let objToString = "";
   for (let key in result) {
     if (result.hasOwnProperty(key)) {
       objToString  = `${key}${result[key]}`;
     }
   }
   return objToString;
}

//TEST
console.log('numbers', process('826'));
console.log('chars', process('**************'));
console.log('caps', process('ABC'));
console.log('spaces', process('AC  DC'));
console.log('mix', process('aaaa7682647828467823486^^&*^&*@^*@&*@^*@^*&^*&@^*&ssw1231323&*(&*(S&(*(*S(S&(23232132wwaaaabbbccccccccdddddd'));
console.log('mix', process('aabbbbccccdddddd'));

CodePudding user response:

hope this helped.

function process (str) {
    if (!str) return "";

    str = str.replace(/[\s\W\d]/g, '').toLowerCase();

    const result = [...new Set(str.split(""))].reduce((p, c) => {
        return p  = c   (str.split(c).length - 1);
    }, "");

    return result;
}

CodePudding user response:

Added a filter that can be modified via second parameter.

function chars(string, filter = 'all') {
  if (!string) return "";
  let rgx;
  switch (filter) {
    case 'a': // Alphabet
      rgx = /[A-Za-z]/;
      break;
    case 'd': // Digits
      rgx = /[\d]/;
      break;
    case 'u': // Uppercase
      rgx = /[A-Z]/;
      break;
    case 'l': // Lowercase
      rgx = /[a-z]/;
      break;
    case 's': // Whitespace
      rgx = /[\s]/;
      break;
    case 'x': // Other
      rgx = /[!`~@#$%&*\^\\\-\][_;:<>,. =(){}|?/]/;
      break;
    case 'all': // All
      rgx = /[!`~@#$%&*\^\\\-\][_;:<>,. =(){}|?/\s\dA-Za-z]/;
      break;
    default:
      break;
  }
  let result = string.split('').reduce((total, char) => {
    let letter = rgx.test(char) ? char : '';
    total[letter] ? total[letter]   : total[letter] = 1;
    return total;
  }, {});
  // Filter all ''
  result = Object.fromEntries(Object.entries(result).filter(([k, v]) => k != ''));

  let objToString = "";
  for (let key in result) {
    if (result.hasOwnProperty(key)) {
      objToString  = `${key}: ${result[key]}, `;
    }
  }
  return objToString.slice(0, -2);
}

//TEST
console.log('numbers', chars('826', 'd'));
console.log('chars', chars('**************', 'x'));
console.log('caps', chars('AaBCCCc', 'u'));
console.log('spaces', chars('AC  DC', 's'));
console.log('all', chars('aaaa7682647828467823486^^&*^&*@^*@&*@^*@^*&^*&@^*&ssw1231323&*(&*(S&(*(*S(S&(23232132wwaaaabbbccccccccdddddd'));
console.log('letters', chars('aaCCbbbBccccdddddd', 'a'));

  • Related