diff --git a/9. Anagram/anagramChecker.js b/9. Anagram/anagramChecker.js
new file mode 100644
index 0000000..2b9dd43
--- /dev/null
+++ b/9. Anagram/anagramChecker.js
@@ -0,0 +1,5 @@
+function anagramChecker(a, b) {
+ return a.toLowerCase().split('').sort().join('') === b.toLowerCase().split('').sort().join('');
+}
+
+anagramChecker("Listen", "Silent");
diff --git a/README.md b/README.md
index 865863b..46cd3d2 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
# Algorithmic_javascript
-> ## This is open-source Project and Part HACKTOBERFEST 2020
+> ## This is open-source Project and Part HACKTOBERFEST 2020
#### Contribute to open source and make the world *A Better Place*
@@ -34,9 +34,9 @@
#### Rules
-* To earn your Hacktoberfest tee or tree reward, you must register and make four valid pull requests (PRs) between October 1-31 (in any time zone).
+* To earn your Hacktoberfest tee or tree reward, you must register and make four valid pull requests (PRs) between October 1-31 (in any time zone).
* PRs can be made to any public repo on GitHub, not only the ones with issues labeled Hacktoberfest. If a maintainer reports your pull request as spam or behavior not in line with the project’s code of conduct, you will be ineligible to participate.
-* This year, the first 70,000 participants who successfully complete the challenge will be eligible to receive a prize.
+* This year, the first 70,000 participants who successfully complete the challenge will be eligible to receive a prize.
@@ -64,9 +64,9 @@ __If you want to contribute then follow theses steps__
> 5.1 A short Introduction
> 5.2 The challenge
> 5.3 Algorithmic thinking
- > 5.4 Code Implementation
+ > 5.4 Code Implementation
6. add two hr tags after each problem in README.md file
-
+
### Star the repository If you enjoyed contributing to open source projects.
@@ -74,13 +74,14 @@ __Algorithms practiced using JS__
## List of problems
1. String reversing
-2. Vowel counter
+2. Vowel counter
3. Finding the Most Recurring Character
4. Sentence Capitalization
5. Palindromes
6. Pig Latin
7. Deep Compare
8. Binary Search Tree
+9. Anagrams
## Explanation
1. String reversing
@@ -112,7 +113,7 @@ We will now explore ways to solve this challenge below. They are:
*1.Chaining in-built methods:*
-
+
The **.split()** method is used to split a string into an array of characters. It receives one argument which specifies the separator that determines where every split occurs.
@@ -164,7 +165,7 @@ __Algorithmic Thinking:__
After reading the problem statement, __ given text > A function is a block of organized, reusable code that is used to perform a single, related action. They may or may not accepts value as parameter for computational tasks. The values are also called as Arguments. -*Let's Breakdown the approach* +*Let's Breakdown the approach* * write a function which receives a parameter called "text". It should be string of any desired length which is to be passed in function as an argument. @@ -175,7 +176,7 @@ __Algorithmic Thinking:__
After reading the problem statement, __ given text
* Function will return the number of vowels found. So you have to use __*return*__ function which stops the the function execution and returns a value.
-__Code Implementation:__
+__Code Implementation:__
We are going to use Two approaches for solving this problem:
1. Using Iterative approach @@ -206,7 +207,7 @@ console.log(vowelcnt("i love HacktoberFest")) **Breaking-down the steps:** -* Firstly, we declard const vowel which is array of five vowels. +* Firstly, we declard const vowel which is array of five vowels. * We declare a function and initialize the counter. * We make use of For loop to itearte over the given string. Next, we convert the all letters of string to lowercase as we don't want to miss out on uppercase letters. * In for loop,use if to check if selected letter is included in the array of vowels which we declared earlier. We call *includes()* method on array of vowels to check whether the array includes selected letter or not. @@ -229,7 +230,7 @@ __The challenge:__ Given a string of text, find and return the most recurring ch ```js maxRecurringChar('aabacada') // will return 'a' -``` +``` __Algorithmic Thinking:__ From the challenge statement, we can infer that our function has only one parameter; the string of text.
We need to somehow keep track of every character in the string as well as the number of times it exists.
This we would describe as character mapping. Once we successfully create such a map, we can easily determine which character has the highest occurence.
Often during presentation situation arises where we need to manipulate the letter casing of strings within our application. Javascript offers two popular methods designed for this purpose:
1.toUpperCase(): this method returns the string passed in as an argument converted in uppercase letters.
-
- 2.toLowerCase(): this method returns the string passed in as an argument converted to lowercase letters.
+
+ 2.toLowerCase(): this method returns the string passed in as an argument converted to lowercase letters.
__The challenge:__ Given a sentence containing two or more words, return the equivalent of the sentence when capitalized. E.g
```js
@@ -314,7 +315,7 @@ capitalSentence("a cat and a dog") // would return "A Cat And A Dog"
__Algorithmic Thinking:__
At a glance this may seem like a very simple challenge, but it actually isn’t when considered carefully. -Working through the challenge, it seems that we need to write a function that'd receive the sentence to be converted as an argument +Working through the challenge, it seems that we need to write a function that'd receive the sentence to be converted as an argument Next,we go through every word in sentence and capitilize every first letter of word. This brings concept of LOOP to mind
@@ -335,19 +336,19 @@ __Code Implementation:__ }); return capsarray.join(' ') - + } - console.log(sentenceCap("ARTIFICIAL")) + console.log(sentenceCap("ARTIFICIAL")) //will return Artificial ``` -+
* We call the .toLowerCase() method on the string of text received to convert the entire sentence to lowercase. We also chain the .split() method in sequence to divide the lowercase sentence into an array of words as shown below. This array is stored as wordsArray
-* Next, using the .foreach() method, we iterate through every word(element) in the array and execute a function on it.
+* Next, using the .foreach() method, we iterate through every word(element) in the array and execute a function on it.
* The function takes the first letter of the word and turns it to uppercase using the .toUpperCase() method. To retrieve the remaining part of the word in lowercase, we use the .slice() method to slice the string starting from position 1 till the end.
* We combine the transformed first letter and the sliced section together to form the capitalized word which we push into our array of capitalized words capsArray.
@@ -388,7 +389,7 @@ __Code Implementation:__
5. Palindromes
-
+
*What is a Palindrome:* A palindrome is a word, number, or other sequence of characters which reads the same backward as forward, such as "madam" or "racecar".
__The challenge:__
Given a string of text, return true or false indicating whether or not the text is a palindrome. e.g
@@ -414,7 +415,7 @@ __code Implementation:__In this challenge, we'd consider two, yet three wa 1. An Intuitive Approach: ```js - + function palindromeCheck(text){ var reverseText= text.toLowercase().split(' ').reverse().join(' ' ) @@ -424,7 +425,7 @@ __code Implementation:__
In this challenge, we'd consider two, yet three wa Let's unveil the "mysteries": * Firstly, the function accepts the string that is to be tested -* Next, all the letters are converted to lowercase,then .split() method is called on string that is received as text +* Next, all the letters are converted to lowercase,then .split() method is called on string that is received as text * Next, we call .reverse() to re-order the array elements in reverse. * After that .join() is called on reversed array to form a whole string. @@ -434,7 +435,7 @@ Let's unveil the "mysteries": 2. Looping Through and Comparing Characters: This could be a bit confusing than the previous implementation. - We will break it into simple steps.Stay in the game. + We will break it into simple steps.Stay in the game. * For example, If we have to test string "machine", we will compare "m" with "e", because if the string is reversed then "e" will take m's position * Similarly, "a" will be compared to "n". @@ -455,7 +456,7 @@ Let's unveil the "mysteries": ``` Let's review it: -* First we convert the string to lowercase and after it we use .split() method +* First we convert the string to lowercase and after it we use .split() method * We use special array method .every() to loop through array & perform our check. In fact,.every() method tests whether all elements pass the test or not which is implemented by provided function @@ -509,7 +510,7 @@ On the other hand, a declarative approach would abstract this process, allowing [Source](https://scotch.io/courses/the-ultimate-guide-to-javascript-algorithms/pig-latin) -__code Implementation:__ +__code Implementation:__ 1. Imperative Approach @@ -603,18 +604,18 @@ Note that we chain both .replace() methods in succession such that both cases ar Comparing objects can be troublesome, not to mention multi-dimensional objects/arrays. Here is something simple to help. -__The challenge:__
- JS Objects, contrary to the way we perceive it, are simply pointers to the data stored, rather than the actual data itself. Thus, to compare objects/arrays a and b we cannot just use normal comparison operators.
-```js +__The challenge:__- JS Objects, contrary to the way we perceive it, are simply pointers to the data stored, rather than the actual data itself. Thus, to compare objects/arrays a and b we cannot just use normal comparison operators.
+```js a === b //false ```- Use of multidimensional objects/arrays is possible, making it difficult to compare simply by iteration since we don't know the depth of a value.
- Different data types like Dates and undefined must also be taken into consideration.
- +
Given the above, return a boolean signifying whether a and b are equivalent in content.
__Algorithmic Thinking:__As we would be comparing each item contained in the objects, a loop may be the first instinct to solving it. However, with the potential of multidimensional iterables, we would have to disect nested arrays in the same way when we encounter them. A combination of iteration and recursion is therefore necessary. So for each item of the array a data type check is necessary as well, to allow execution of a relevant comparison. - + Breaking it down: * check if ```a === b``` * check if ```a``` and ```b``` are both iterable @@ -622,17 +623,17 @@ __Algorithmic Thinking:__
As we would be comparing each item contained in the
-__code Implementation:__+__code Implementation:__
Firstly, we'll do the most simple check of ```a === b``` to avoid unnecessary complexity. This will process all of the equal literal values for us. -```js +```js if(a === b) return true ``` Then comes the interesting part! There are several data types we need to look out for: Objects, Arrays(which JS treats as an object), and Dates(which is also treated as an object!), thus all we have to do is check if both a and b are of type object. If not, we can just return false as they didn't pass the ```a === b``` test. -```js +```js if(typeof a === "object" && typeof b === "object")... ``` @@ -640,8 +641,8 @@ Note that we use ```===``` here to differentiate between data types strictly. Next, we can process the dates first, as that doesn't require iteration. Make sure to compare ```Date.valueOf()``` instead of the date object itself. -```js -if(a instanceof Date && b instanceof Date) return a.valueOf() === b.valueOf() +```js +if(a instanceof Date && b instanceof Date) return a.valueOf() === b.valueOf() ``` Lastly, by taking the keys of the iterables we can compare the length of ```a``` and ```b```, then make use of built-in Array.some method to check if any values of the two iterables don't match. @@ -652,10 +653,10 @@ const keysA = Object.keys(a) //make sure a and b are the same length if(keysA.length !== Object.keys(b).length) return false - + //Array.some() iterates through the values in an array and stops executing nested code until there is one that returns true //in this case that would be when there is one different value between a and b -return !keysA.some( key => { +return !keysA.some( key => { //run deepCompare recursively return !deepCompare(a[key], b[key]) }) @@ -671,7 +672,7 @@ const deepCompare = (a, b) => { if(typeof a === "object" && typeof b === "object") { if(a instanceof Date && b instanceof Date) return a.valueOf() === b.valueOf() - else + else { const keysA = Object.keys(a) if(keysA.length !== Object.keys(b).length) return false @@ -683,7 +684,7 @@ const deepCompare = (a, b) => { else return false } -deepCompare(1, 2) +deepCompare(1, 2) //false deepCompare({"first": 1, "second": 2}, {"first": 1, "second": 2}) @@ -693,16 +694,17 @@ deepCompare([1, "2", 3.0], [1, "2", 3]) //false const arr = [1, 2, "3", [{4: "5", 6: 7}, 8.0, new Date(), undefined]] -deepCompare(arr, [1, 2, "3", [{4: "5", 6: 7}, 8.0, new Date(), undefined]]) +deepCompare(arr, [1, 2, "3", [{4: "5", 6: 7}, 8.0, new Date(), undefined]]) //true ``` - + It's that simple! Hope this helps.
Given 2 strings of text, write an algorithm that return true or false indication wheter or not those string are anagrams. eg.
+```js +anagramChecker("Listen", "Silent"); +``` + +__Algorithmic Thinking:__Our function must receive 2 parameters, as we need to compare 2 strings.
+Function should check if words are made of the same letters. Most straightforward approach seems to be something like in the most recurring character problem, but it will lead to long and complicated code.
+Much simpler code could be written using build in JS functions when we realize that characters order in words does not matter (as stated in anagram definition), so if we sort them in boths strings and compare - we will know if they are anagrams or not.
+ +__code Implementation:__To use build in `sort` method on string we must change it into array, which can be done using `split` method with `''` as argument.
+```js +string.split('').sort() +``` +Then we must change this new array again into string, using `join`
+```js +string.split('').sort().join() +``` +We should also notice that strings should be compared case insensitive, so we need to 'normalize' their case
+```js +string.toLowerCase().split('').sort().join() +``` +When we do those thing for both strings - we can check if they are eqaual - which will tell us if they are anagrams:
+```js +function anagramChecker(a, b) { + return a.toLowerCase().split('').sort().join('') === b.toLowerCase().split('').sort().join(''); +} +``` + +11. Name __The challenge:__@@ -983,7 +1017,7 @@ __code Implementation:__