Created
February 21, 2017 22:21
-
-
Save basekays/9c6ee621509aa2c314d98cba489959c8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 `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