Last active
January 4, 2016 04:02
-
-
Save brianswisher/42dfbbf9a8631841380b to your computer and use it in GitHub Desktop.
Common Characters
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
| ((libs) => { | |
| var promises = []; | |
| libs.forEach((src) => { | |
| promises.push(new Promise(resolve => { | |
| if (!document.querySelector(`[src="${src}"]`)) { | |
| const script = document.createElement("script"); | |
| script.src = src; | |
| script.async = false; | |
| script.onload = () => { | |
| resolve(); | |
| }; | |
| document.head.appendChild(script); | |
| } else { | |
| resolve(); | |
| } | |
| })); | |
| }); | |
| function commonChars(a, b) { | |
| var input = (function(){ | |
| var input1 = a.split(""), | |
| input2 = b.split(""), | |
| scan = {}, | |
| filter = {}; | |
| return { | |
| scan: function(){ | |
| input1.forEach(function(c){ | |
| scan[c] = c; | |
| }); | |
| return this | |
| }, | |
| filter: function(){ | |
| input2.forEach(function(c){ | |
| if(scan[c]) filter[c] = c; | |
| }); | |
| return this | |
| }, | |
| get: function(){ | |
| return Object.keys(filter) | |
| } | |
| } | |
| })(a, b); | |
| return input. | |
| scan(). | |
| filter(). | |
| get() | |
| } | |
| return Promise.all(promises) | |
| .then(() => { | |
| expect(commonChars("a", "b").join("")).toEqual(""); | |
| expect(commonChars("a", "ba").join("")).toEqual("a"); | |
| expect(commonChars("a", "baa").join("")).toEqual("a"); | |
| expect(commonChars("ca", "bcaa").join("")).toEqual("ca"); | |
| expect(commonChars("Hello there, How are you?", "I'm good, thanks.").join("")).toEqual(" o,tha"); | |
| console.log("All tests passed."); | |
| }); | |
| })([ | |
| "https://wzrd.in/standalone/expect@latest" | |
| ]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment