Algorithm Problems - Baby Steps in Providing Solutions Using JavaScript
Tags: beginners, javascript, algorithms, tutorial
Published: 2021-07-19 10:50:05

## Introduction
Recruiters who are interested in the problem solving skills of their candidate, present them problems and ask them to write an algorithm for that problem. By so doing, recruiters are able to separate those who just copy and paste codes from those who really understand the syntax and how it works. But What is an Algorithm?

### JUMP TO:
* [Steps in Solving an Algorithm Problem](#1)
* [25 Algorithm to Sharpen Your Problem Solving Skills](#3)
* [Bonus](#4)
* [Conclusion](#4)

Algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem, based on conducting a sequence of specified actions. A computer program can be viewed as an elaborate algorithm. In mathematics and computer science, an algorithm usually means a small procedure that solves a recurrent problem - [whatis](https://whatis.techtarget.com/definition/algorithm).

From the definition above, we can deduce that algorithm is used to solve problems and for one to build an effective algorithm, the person must have a good understanding of the problem being solved.

 ![Solving Problems With Algorithm](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzto8gd1wyx01tjd1ieu.jpg)

Algorithm or Problem Solving interviews are almost unavoidable today with the current trends in technology. 

![Algorithm Interviews](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bujnnihdzgvm4icmxgk3.png)

In view of this reality, how can you achieve an algorithm to solve a problem?

<a name="1"></a>
### Steps in Solving an Algorithm Problem
![Steps in Problem Solving](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6gidy88wsnragk2h5qug.jpg)

1. **UNDERSTAND** - Try to understand the problem and break it down into the smallest parts possible.
2. **STRATEGIZE** - Find out what tools are available for you in your programming language *(We are using JavaScript for the purpose of this article)*.
3. **IMPLEMENT** - Solve each of the small problems with those tools and combine the solutions to form one solution to the whole problem.

> For Example, if you are presented with a *word palindrome* problem, how do you go about writing the algorithm to that problem?

## Let's apply the steps above
![Applying the Steps for problem Solving](https://media.giphy.com/media/3oz8xW1WD6hLm1oHHq/giphy.gif)

### **STEP A:** UNDERSTAND
The big problem here is to check if the word we are given is the same if reversed. 

However, for us to make that happen, we have to provide solutions to smaller problems like:
* How to keep a reference to the original word we are given.
* How to reverse the word.
* But for us to reverse the word we need to split the word into characters.
* If we eventually reverse it, we need to join the characters
* Finally, we need to compare the new word to the old one

### **STEP B:** STRATEGIZE

Some of the tools We have at our displosal include the following:
* variables
* .split()
* spread operator
* if...else... conditional statement
* ternary operator
* .reverse()
* .join()

### **STEP C:** IMPLEMENT

Having broken down this problem into smaller bits, we have a way to keep a copy of the original word given to us using the ``variable`` tool provided for us by JavaScript

```javascript

const orginalWord = word;

```
Since we cannot just reverse a string, we need to split the word and make it an array using the ``.split("")`` method

```javascript

let splittedWord = word.split("");

```

Now we can reverse the array using the ``.reverse()`` like so:

```javascript

let reversedWord = splittedWord.reverse();

```

It is now reversed. But we need to return it to a string without anything separating the characters. So we can use the ``.join("")`` to do that:

```javascript

let newWord = reversedWord.join("");

```
Finally, let's compare the word we were given initially, and the one we have now reversed

```javascript

   if(orginalWord === newWord){
     console.log(true)
   }else{
     console.log(false)
   }

```

### Final Code
```javascript

function palindrome(word){
  const orginalWord = word;
   let splittedWord = word.split("");
   let reversedWord = splittedWord.reverse();
   let newWord = reversedWord.join("");

   if(orginalWord === newWord){
     return(true)
   }else{
     return(false)
   }
}

```
Someone else can solve the problem with the following code:

```javascript

function palindrome(word){
   return(word === [...word].reverse().join(""))
}

```
> Notice how easy it is to Conquer any problem if you divide it. So basically, algorithm is all about how you can divide and conquer!

Practice, they say breeds perfection. So with that in mind, I am presenting you with the following series to help you get better in your problem solving skills. The series was made a while ago when I decided to embrace algorithm. That decision has improved my programming skills generally and I am sure that it will enhance yours too as well as you chances of winning at technical interviews.

> So please, enjoy the series...

![enjoy the series](https://media.giphy.com/media/3o7527WMiaRJmrWMTe/giphy.gif)

<a name="2"></a>
## 25 Algorithm to Sharpen Your Problem Solving Skills
1. [Algorithm 101: 7 Ways to Reverse a String](https://dev.to/ebereplenty/algorithm-101-7-ways-to-reverse-a-string-15cb)

2. [Algorithm 101: 13 Ways to Count Vowels in a String](https://dev.to/ebereplenty/algorithm-101-13-ways-to-count-vowels-in-a-string-de5)

3. [Algorithm 101: 6 Ways to Find the Most Recurring Character in a String](https://dev.to/ebereplenty/algorithm-101-6-ways-to-find-the-most-recurring-character-in-a-string-5036)

4. [Algorithm 101: 8 Ways to Capitalize a Sentence](https://dev.to/ebereplenty/algorithm-101-8-ways-to-capitalize-a-sentence-3bia)

5. [Algorithm 101: 6 Ways to Check if a Word is a Palindrome](https://dev.to/ebereplenty/algorithm-101-6-ways-to-check-if-a-word-is-a-palindrome-40a3)

6. [Algorithm 101: 3 Ways to Find Hamming Distance](https://dev.to/ebereplenty/algorithm-101-3-ways-to-find-hamming-distance-45mo)

7. [Algorithm 101: 6 Ways to Find the Longest Word in a Sentence](https://dev.to/ebereplenty/algorithm-101-6-ways-to-find-the-longest-word-in-a-sentence-3d89)

8. [Algorithm 101: 9 Ways to Search and Replace a Word](https://dev.to/ebereplenty/algorithm-101-9-ways-to-search-and-replace-a-word-24jk)

9. [Algorithm 101: 3 Ways to Check If Two Words are Anagrams](https://dev.to/ebereplenty/algorithm-101-3-ways-to-check-if-two-words-are-anagrams-1j6l)

10. [Algorithm 101: 3 Ways to Create Pig Latin](https://dev.to/ebereplenty/algorithm-101-3-ways-to-create-pig-latin-513m)

11. [Algorithm 101: 3 Ways to Check if Two Sentences are Anagrams](https://dev.to/ebereplenty/algorithm-101-3-ways-to-check-if-two-sentences-are-anagrams-55jc)

12. [Algorithm 202: Array Chunking in 3 Ways](https://dev.to/ebereplenty/algorithm-202-array-chunking-in-3-ways-1ank)

13. [Algorithm 202: Array Merging Without Duplicates in 4 Ways](https://dev.to/ebereplenty/algorithm-202-array-merging-without-duplicates-in-4-ways-17h3)

14. [Algorithm 202: Falsy Bouncer in 4 Ways](https://dev.to/ebereplenty/algorithm-202-falsy-bouncer-in-4-ways-36p4)

15. [Algorithm 202: Where I Belong in 3 Ways](https://dev.to/ebereplenty/algorithm-202-where-i-belong-in-3-ways-1chk)

16. [Algorithm 101: 3 Ways to Reverse an Integer](https://dev.to/ebereplenty/algorithm-101-3-ways-to-reverse-an-integer-24gn)

17. [Algorithm 202: 3 Ways to Sum a Range of Values](https://dev.to/ebereplenty/algorithm-202-3-ways-to-sum-a-range-of-values-5g93)

18. [Algorithm 202 (My Interview Question): Grouping Anagrams in 3 Ways](https://dev.to/ebereplenty/algorithm-202-my-interview-question-grouping-anagrams-in-3-ways-49i4)

19. [Algorithm 101: 4 Ways to FizzBuzz a Single Number](https://dev.to/ebereplenty/algorithm-101-4-ways-to-fizzbuzz-a-single-number-380d)

20. [Algorithm 101: 2 Ways to FizzBuzz a Range of Numbers](https://dev.to/ebereplenty/algorithm-101-2-ways-to-fizzbuzz-a-range-of-numbers-8f8)

21. [Algorithm 101: 3 Ways to Get the Fibonacci Sequence](https://dev.to/ebereplenty/algorithm-101-3-ways-to-get-the-fibonacci-sequence-1i1c)

22. [Algorithm 101: 2 Ways to Find the Largest Product Yielded by 3 Integers](https://dev.to/ebereplenty/algorithm-101-2-ways-to-find-the-largest-product-yielded-by-3-integers-fpf)

23. [Algorithm 101 (Interview Question): 2 Ways to Determine if 2 Words are Isomorphic ](https://dev.to/ebereplenty/algorithm-101-interview-question-2-ways-to-determine-if-2-words-are-isomorphic-11ap)

24. [Algorithm 202 (Interview Question): Matching Parenthesis in 2 Ways](https://dev.to/ebereplenty/algorithm-202-interview-question-matching-parenthesis-in-2-ways-3381)

25. [Algorithm 202: Sock Merchant](https://dev.to/ebereplenty/hackerrank-sock-merchant-1cgh)

![BONUS](https://media.giphy.com/media/66c2LdxdRxu4E/giphy.gif)

<a name="3"></a>
## BONUS
[How Many Tutorials Before I become a Pro? - Newbie Asks](https://dev.to/ebereplenty/how-many-tutorial-before-i-become-a-pro-newbie-asks-1fa)

[3 Things to Help You Land Your First Software Engineering Job](https://dev.to/ebereplenty/3-things-to-help-you-land-your-first-software-engineering-role-46hn)

<a name="4"></a>
### Conclusion
With the forgoing, you can see that algorithm isn't as difficult as it looks. Basically, it is all about solving day to day problems. However without practicing, it will not be easy to solve problems easily.

So I encourage you to follow and practice with the algorithm series presented above for you.

[Check out some of the solutions on github](https://github.com/EBEREGIT/Algorithms)