Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // GOAL: to return all the unique characters in a given string and how many
- // times each letter was found. Needs to return an object that can represent this.
- //
- // take the given string and remove the spaces
- // iterate through the string and select a chosen letter
- // create an object to be returned
- // assign the letter as a property to the object
- // add the instances of the chosen letter to the appropriate property value
- // repeat for all the letters in the string
- // return the object to be visulized
- function countLetters (str) {
- var noSpaces = str.replace(/\s/g, '');
- var cleanString = noSpaces.toLowerCase();
- var finalObj = {
- };
- for (var i = 0; i < cleanString.length; i++){
- if (finalObj[cleanString[i]]) {
- finalObj[cleanString[i]] += 1;
- } else {
- finalObj[cleanString[i]] = 1;
- }
- }
- return finalObj;
- }
- console.log(countLetters("lighthouse in the house"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement