0

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 as i dont like "converting an object to string" part.

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'));

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.