Problem Solving in JavaScript
By: Mowmita Ahmed
Tags: Programming Interviews, Programming Languages, Javascript Development, Javascript Tips, JavaScript

Photo by Luca Bravo on Unsplash

Today we’ll discuss about some common problem solving in JavaScript. We’ll solve 10 problems here.

(1) Find Factorial of a number using for loop:

Find factorial of a number using for loop

Consider we want to find the factorial of 20 using for loop. So we have kept 20 as a number in num variable as constant. What is the definition of factorial number? In mathematics, factorial number is the multiplication of all the positive integers less than or equal to a given number. So we came to know this we need to multiply numbers in this program. so we considered variable fact equal to one. We have to use for loop here. In for loops condition we have to set i equal to one. In for loop the value of the fact variable will be multiplied with the value of i. The loop will continue with the value of num. finally the factorial value will be stored in fact variable and printer.

(2) Find Factorial of a number using a while loop:

Find Factorial of a number using a while loop

From the above we already know about the definition of factorial of a number. Now we’ll discuss about the process of finding the factorial of a number using while loop . For this we’ll use while loop. Here we’ll find out the factorial of 5 and we’ll store it in variable fact . We initialized i equal to one. We have created a while loop. It check the condition is i is less than or equal to the num or not. If it is it will multiply with the fact and store it in fact then it’ll increase i by 1 till then. After fulfilling the condition the program will print the factorial of the number .

(3) Calculate Factorial in a Recursive function:

Factorial in a Recursive function

Recursive function is a function that calls itself during execution. Here factorial is a function which accepts only one parameter. We want to find out the factorial of 10 so we called the factorial function with an argument. In this function it accepts parameter named n . which is checked under if…else condition. First it checked under if condition , if n is equal to 0 it returns 1. If it don’t fulfill the condition it enters into the else and return the multiplication of n and the factorial function itself . Outside of the function there in the program it prints the result.

(4) Create a Fibonacci Series using a for loop:

Fibonacci Series using a for loop

Fibonacci series is a series in which each number is the sum of two preceding number. Example: 1, 1, 2, 3, 5, 8, 13 etc. On above program fibonacci is a function accepts an argument named n. In this function fibo is an array . In for loop there is a condition which starts with i equal to 2 and continues to until when it’s not equal to n. In each condition it saves the sum of two preceding number in the i index array. Then the fibonacci function returns the fibo . Outside the function after calling it , it prints the result.

(5) Find Fibonacci Element in a Recursive Way:

Find Fibonacci Element in a Recursive Way

We already know the definition of fibonacci series. Here in this program we’ll find out the sum of the fibonacci series. Here fibonacci is a function which accepts an argument named n. After that it checks is it equal to zero. If it , it’ll return 0. If it’s equal to 1 it’s return 1 . If it’s not equal to 0 neither 1 it return the sum of two numbers sum where the fibonacci function is being calling recursively. After fulfilling the conditions after calling the function it prints the result.

(6) Check whether a number is a Prime Number or not:

Check whether a number is a Prime Number or not

We know that prime number is a number which is divisible by only itself and 1. Here with this program we’ll check that the given number is prime or not. In this program isPrime is a function which checks the number. Which accepts an argument named n . We know that 0 and 1 are prime numbers. So in for loop we started with 2. In which the condition is if the number is equal to 2 or less then given number it’ll play the for loop. after passing the for loop it’ll check if it’s reminder is equal to zero. If it’s it’ll return It’s not a prime number. If the given number not fulfills the condition of for loop it’ll return your number is a prime number. outside of the functions definition after calling the function this program prints the result.

(7) Find the largest element of an array:

Find the largest element of an array

We can easily find the largest element of an array with an simple function of Math. It’s Math.max() . It’s returns the largest element of the array.

(8) Sum of all numbers in an array:

Sum of all numbers in an array

We can easily sum up of an array with the reduce method in JavaScript. This method executes reducer method in the each elements of the array returns a single output .Here in the above program the reducer returned sum of each element of the array.

(9) Reverse a string:

Reverse a string

Here we have reversed a string using this program. We have wrote a function named reverseNow which accepts an argument named string and returns the reverse form of the given string. Here we used three methods of string split, reverse and finally join.

(10) Remove duplicate item from an array:

Remove duplicate item from an array

Here in this program we have used the split method to count the total number of word in the string . First we have set a string in a variable named str. Then split the str string using split method which split the string and returns some substring as array .And then we counted the length of the array. And then we have printed it .