UNPKG

ds-algo-study

Version:

Just experimenting with publishing a package

27 lines (24 loc) 631 B
/*Function to check how many times an alpha-numeric character occurs in a string. Case insensitive. */ let isAlphaNumeric = (char) => { char = char.toString(); let id = char.charCodeAt(0); if ( !(id > 47 && id < 58) && // if not numeric(0-9) !(id > 64 && id < 91) && // if not letter(A-Z) !(id > 96 && id < 123) // if not letter(a-z) ) { return false; } return true; }; let countChars = (string) => { let obj = {}; for (let char of string) { if (isAlphaNumeric(char)) { char = char.toLowerCase(); obj[char] !== undefined ? ++obj[char] : (obj[char] = 1); } } return obj; };