Created
May 8, 2021 09:10
-
-
Save fcole90/b8577095d79fc9af4c542db193c85b09 to your computer and use it in GitHub Desktop.
Solution to mock interview https://youtu.be/BpGh-fKRYiQ?t=1160
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
| const myWord = "sprot" | |
| const dictionatyOfWords = { | |
| "sport": "pop", | |
| "ports": "pop2", | |
| "port": "A winter sport", | |
| "sports": "A marionette", | |
| "trops": "A marionette", | |
| } | |
| const wordMatch = (word, map) => { | |
| const wordList = Object.keys(map) | |
| let lettersList = word.split("") | |
| return wordList.filter((possibleWord) => { | |
| console.log("Testing:", possibleWord, "-", word) | |
| if (possibleWord.length !== word.length) { | |
| console.log("Not same len:", possibleWord) | |
| return false | |
| } | |
| let possibleWordList = possibleWord.split("") | |
| for (let letter of lettersList) { | |
| let found = false | |
| for (let i = 0; i < possibleWordList.length; ++i) { | |
| const possibleLetter = possibleWordList[i] | |
| console.log("Comparing:", letter, possibleLetter, "/", possibleWordList) | |
| if (letter === possibleLetter) { | |
| console.log("Match!") | |
| console.log("Removing letter:", letter) | |
| possibleWordList = possibleWordList.reduce((acc, cur, j) => ( | |
| i === j ? acc : [...acc, cur] | |
| ), []) | |
| console.log("Now is:", possibleWordList) | |
| found = true | |
| break | |
| } | |
| } | |
| if (found !== true) { | |
| console.log("Not the same...") | |
| return false | |
| } | |
| } | |
| console.log("Same!") | |
| return true | |
| }) | |
| } | |
| console.log(wordMatch(myWord, dictionatyOfWords)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment