Last active
December 20, 2015 23:59
-
-
Save hauntedhost/6217011 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Write a function to get the common letters out of two strings | |
// commonLetters('hello world', 'hello moon') => should return 'helo' | |
function commonLetters(str1, str2) { | |
var map = {}, | |
common = ''; | |
for (var i = 0; i < str1.length; i++) { | |
var letter = str1[i]; | |
if (letter != ' ') map[letter] = true; | |
} | |
for (var i = 0; i < str2.length; i++) { | |
var letter = str2[i]; | |
if (map[letter] && common.indexOf(letter) == -1) common += letter; | |
} | |
return common; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment