Skip to content

Instantly share code, notes, and snippets.

@basekays
Created February 21, 2017 22:21
Show Gist options
  • Select an option

  • Save basekays/9c6ee621509aa2c314d98cba489959c8 to your computer and use it in GitHub Desktop.

Select an option

Save basekays/9c6ee621509aa2c314d98cba489959c8 to your computer and use it in GitHub Desktop.
/**
* Write a function `f(a, b)` which takes two strings as arguments and returns a
* string containing the characters found in both strings (without duplication), in the
* order that they appeared in `a`. Remember to skip spaces and characters you
* have already encountered!
*
* Example: commonCharacters('acexivou', 'aegihobu')
* Returns: 'aeiou'
*
*/
var commonCharacters = function(string1, string2) {
var result = [];
var charactersOfString1 = [];
for (var i = 0; i < string1.length; i++) {
charactersOfString1.push(string1[i]);
}
for (var j = 0; j < charactersOfString1.length; j++) {
if (string2.includes(charactersOfString1[j])) {
if (!result.includes(charactersOfString1[j])) {
result.push(charactersOfString1[j]);
}
}
}
return result.join('');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment