Solving Algorithms and Problems with Javascript

Solving Algorithms and Problems with Javascript
Written by Priyanka JalanMay 7, 2020
7 min read
Frontend Development
12 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 12 VIEWS SHARE
Priyanka Jalan

Fullstack Software Engineer

This blog will help you understand the concept of solving very basic algorithms using javascript.

I am sure you must be learning a lot through tutorials and then forgetting about things easily. And again watching those tutorials over and over again or may be some different one and forget again !!! It becomes easier to get lost in the tutorials hell and without practice your learning gets wasted because when the real problem arrives you don't know when to use it. So don't learn the basics only, solve small problems to practice the fundamentals.

In this tutorial we will learn few basic problem solving using javascript build in functions of string, array, etc.

Problem Solving 1: Reserve a String

This is the most favourite question of interviewers to reverse a string. Well you can solve it in different ways:

Solution 1: Using built in function

Built in functions used:

String.split() : Divides a string into substrings with the separator passed and converts into an array,

Array.reverse() : Reverses the elements of an array,

Array.join() : Creates and returns a new concatenated string of all the elements in the array specified by a string separator.

function stringReverse(str){
     return str.split('') // split each character of the string and return array of characters
               .reverse() // reverse the array of characters
               .join(''); // join the array into string again
}

Solution 2:

Now the interviewer says don't use the reverse() function !! Now what ??

No worries, we have for loop for the rescue, here is how you can use it

function stringReverse(str){
  let reverseString = '';
  for(let i = str.length - 1; i >= 0; i--) { 
    reverseString = reverseString + str[i]; 
  }  
  return reverseString;
}

Solution 3: Solve it smarter

Show your smartness that you know the latest javascript patterns and use reduce

Reduce is a function that executes on each element in the array and results in a single output. It takes two parameters - first: a reducer function to execute on each array element and second - an initial value

function stringReverse(str){
    return str.split('')
              .reduce((reverseString, character) => character + reverseString, '');
}

Problem Solving 2: Count Vowels in a String

Out of the 26 alphabets in english 5 of them are considered as vowels: A, E, I, O, U. (Bonus knowledge : Y is considered as a sixth vowel. One vowel can have many sounds and sometimes can be silent without any sound at all).

Solution 1: Using a old school for loop (Understand the concept)

function countVowels(str){
  const vowels = ['a','e','i','o','u']
  let count = 0;
  for(let i=0; i<str.length; i++){
    if(vowels.indexOf(str[i]) != -1){
      count++;
    }
  }
  return count;
}

Solution 2: Using for of loop (Build upon your fundamental and apply latest trends to get smarter)

function countVowels(str){
  const vowels = ['a','e','i','o','u']
  let count = 0;
  for(let element of str){
    if(vowels.indexOf(element) != -1){
      count++;
    }
  }
  return count;
}

Let's keep learning together.
Comment below for more things you would like to know about Or suggest a different way of solving
javascript
vowels
string
12 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 12 VIEWS SHARE
Was this blog helpful?
You must be Logged in to comment
Code Block

1 Comments

Minaz Uddin

Very helpful blog, and very informative

Minaz Uddin

yes

Priyanka Jalan
Fullstack Software Engineer
1 Blog Posts
0 Discussion Threads
Trending Categories
93
Software12
DevOps34
Frontend Development13
Backend Development13
Server Administration13
Linux Administration12
Data Center14
Sentry11
Terraform15
Ansible9
Docker13
Penetration Testing12
Kubernetes12
NGINX8
JenkinsX10
Jenkins18
SSL6
Ethical-Hacking10
Python8
NodeJs9
RedHat8
Github11
AngularJs15
Google Cloud Platform (GCP)6
SonarQube9
Amazon Web Service (AWS)2
VMware17
Blockchain6
Snipe-IT7