Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. // GOAL: to return all the unique characters in a given string and how many
  2. // times each letter was found. Needs to return an object that can represent this.
  3. //
  4. // take the given string and remove the spaces
  5. // iterate through the string and select a chosen letter
  6. // create an object to be returned
  7. // assign the letter as a property to the object
  8. // add the instances of the chosen letter to the appropriate property value
  9. // repeat for all the letters in the string
  10. // return the object to be visulized
  11.  
  12. function countLetters (str) {
  13. var noSpaces = str.replace(/\s/g, '');
  14. var cleanString = noSpaces.toLowerCase();
  15. var finalObj = {
  16.  
  17. };
  18.  
  19. for (var i = 0; i < cleanString.length; i++){
  20.  
  21. if (finalObj[cleanString[i]]) {
  22. finalObj[cleanString[i]] += 1;
  23. } else {
  24. finalObj[cleanString[i]] = 1;
  25. }
  26. }
  27. return finalObj;
  28. }
  29.  
  30.  
  31. console.log(countLetters("lighthouse in the house"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement