Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function strLetterCount (str){
- // create an object to store each letter
- const countObj = {}
- // iterate over the string
- for (let i = 0; i < str.length; i++) {
- // if the letter isn't stored in the object, add it with a count of one
- if (!countObj[str[i]]) {
- countObj[str[i]] = 1;
- } else {
- // if the letter is already in the object, increment the count
- countObj[str[i]]++;
- }
- }
- // create a string to add the letter / count to
- let outStr = "";
- // iterate over the object to create the string
- for (const letter in countObj) {
- outStr += `${letter}${countObj[letter]}`;
- }
- // return the string
- return outStr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement