|
| 1 | +# 0002 isWordExit ( L-I ) |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +Write a dictionary class, you have a setup function that takes in an array of words and a search function that takes in a word and returns true if the word is in the dictionary and false if it is not. |
| 6 | + |
| 7 | +## Test Cases |
| 8 | + |
| 9 | +- Input: ["hello", "world", "this", "is", "a", "test"], "test", Output: true |
| 10 | +- Input: ["hello", "world", "this", "is", "a", "test"], "how", Output: false |
| 11 | + |
| 12 | +## Solution |
| 13 | + |
| 14 | +```javascript |
| 15 | +class Dictonary { |
| 16 | + constructor(arr) { |
| 17 | + this.dict = new Set(arr); |
| 18 | + } |
| 19 | + |
| 20 | + isInDict(word) { |
| 21 | + return this.dict.has(word) |
| 22 | + }; |
| 23 | +}; |
| 24 | + |
| 25 | +const isExits = new Dictonary(['tea', 'mug', 'jug', 'bug']); |
| 26 | + |
| 27 | +console.log(isExits.isInDict('cat')); |
| 28 | +console.log(isExits.isInDict('bug')); |
| 29 | +``` |
| 30 | + |
| 31 | +## How it works |
| 32 | + |
| 33 | +- We create a class called Dictonary. |
| 34 | +- We create a constructor that takes in an array of words. |
| 35 | +- We create a Set from the array and assign it to a property called dict. |
| 36 | +- We create a method called isInDict that takes in a word. |
| 37 | +- We check if the word is in the dict Set. |
| 38 | +- We return the result. |
| 39 | + |
| 40 | +## References |
| 41 | + |
| 42 | +- [Wikipedia](https://en.wikipedia.org/wiki/Set_(abstract_data_type)) |
| 43 | +- [GeeksforGeeks](https://www.geeksforgeeks.org/set-in-javascript/) |
| 44 | +- [StackOverflow](#) |
| 45 | + |
| 46 | +## Problem Added By |
| 47 | + |
| 48 | +- [GitHub](https://www.github.com/devvsakib) |
| 49 | +- [LinkedIn](https://www.linkedin.com/in/devvsakib) |
| 50 | +- [Twitter](https://twitter.com/devvsakib) |
| 51 | + |
| 52 | +## Contributing |
| 53 | + |
| 54 | +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. |
| 55 | + |
| 56 | +Please make sure to update tests as appropriate. |
0 commit comments