Skip to content

Instantly share code, notes, and snippets.

@bradoyler
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradoyler/9156470 to your computer and use it in GitHub Desktop.
Save bradoyler/9156470 to your computer and use it in GitHub Desktop.
Lesson #2: string manipulation and counting
//reverse characters in string
console.log('test string'.split('').reverse().join('')); // output: gnirts tset
// count characters in a string
function Char_Counts(str1) {
var uchars = {};
str1.replace(/\S/g, function(l){
uchars[l] = (isNaN(uchars[l]) ? 1 : uchars[l] + 1);
});
return uchars;
}
console.log(Char_Counts("test string"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment