8 Common Problems Solving With JavaScript

Zahidul Islam Joy
6 min readNov 5, 2020

JavaScript is one of the most used language in today's world. If you want to be a javascript developer, you have to solve a lot of problems. Here we will discuss 10 basic problems that you should definitely solve as a javascript beginner.

1.Swap Numbers without using Temporary Variable or Destructuring

In most cases, we use a third temporary variable to swap 2 variables. In javascript, we use destructuring to swap variables. But how to swap without these methods? Let’s see how to do it,

var num1 = 10;
var num2 = 30;
console.log(num1, num2); // output: 10, 30num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
console.log(num1, num2) // output: 30, 10

At first, add the 2 numbers and store the value in the first variable. Then deduct the num2 from the newly stored num1. Then store this value in the in num2. At last, deduct the newly stored num2 from num1 and store it as the new value of num1. Now have a look at the console. Your numbers have been swapped! Isn’t it simple?

2.Find The Largest Element of an Array

You have to find the largest elements of an array without using any built-in method of javascript. How to do it?

var array = [56, 6, 42, 12, 25, 3, 761, 33];var max = array[0]for(var i = 0; i < array.length; i++){
if(array[i] > max){
max = array[i];
}
}
console.log('Largest num is:', max); //output : Largest num is: 761

At first, consider the first element of the array as the largest number and store it in a variable named max. Then use a for loop starting from 0 to the length of the array. Use the “if” conditional statement to check if the current value of the array is larger than the max value. If it returns true, set the current value as the largest number in the max variable. This is how for loop will loop through every element of the array and compare it with the max variable. Finally, you get the largest variable present in the array.

3.Reverse a string

You are given a string and you have to reverse it without using any of the javascript string or array method. How will you do it? Let’s see how can we solve this problem.

var str = 'Coding with javascript is fun!';var reverse = "";for(var i = 0; i < str.length; i++) {
reverse = str[i] + reverse;
}
console.log(reverse); //output: !nuf si tpircsavaj htiw gnidoC

Look at this code snippet carefully. Here a variable named reverse is taken which is initially an empty string. Then we run a for loop starting from 0 to the length of the given string. Inside the loop, we are adding the current character of the string with the reverse variable and storing it as the new value of the reverse variable. The tricky part is, we have to add the reverse variable with the current character. If we do, reverse = reverse + str[i]; nothing will be changed. The output will remain same as the given string.

4.Count the number of words in a string

Our task is to count the number of words present in a string. Let’s do it.

var str = 'Coding with javascript is fun!';var count = 0;for(var i = 0; i < str.length; i++) {    if(str[i] == " " && str[i-1] !== " "){
count = count + 1;
}
}
count = count + 1;
console.log(count); //output: 5

Let’s take a variable count which will be the number of words in the string. Initially, it is 0. Then run a for loop. Use a conditional statement. Check if the current character of the string is a white space. Also, check if the previous character of the string is not a white space. If both conditions satisfied then increase the count value +1. This will count the word based on white space. But we have 4 white spaces in our string. For that reason, the last word will not be counted. So we add +1 to the count variable after finishing the for loop.That’s it!

5.Remove Duplicate Element From Array

It is one of the most common problem in programming. You have to remove all the duplicate elements from an array.

var ara = [5,4,3,5,1,6,3,5,7,45,3,2,6,7,9,5,4];var result =[];for(var i=0; i<ara.length; i++){    if(result.indexOf(ara[i]) === -1){      result.push(ara[i]);
}
}
console.log(result)

At first, we have to take an empty array. Then loop through the given array elements. With indexOf() method check if the current element is present in the result array. If it is not present in the result array, indexOf() will return -1. Then just push this current element to the result array. Don’t use result[i] = ara[i] . It will leave unwanted holes in the result array, as the duplicate elements will not be added to the result array. So, it will initialize the duplicate element position with undefined in the result array. Always use push and pop to insert and delete elements from an array.

6.Calculate Factorial Using Recursion

The problem is to determine the factorial of a given number. You can solve it by using a loop. But how to solve it by using a recursive function? So, what is a recursive function? A recursive function is a type of function that calls itself repeatedly during code execution until it reaches the break condition.

function factorial(n){
if(n === 0){
return 1;
}
else{
return n * factorial(n - 1);
}
}
var result = factorial(5);
console.log(result); //output: 120

In recursive function, at first, you have to return something for the base case which will be the break condition. The base case is the last call of that recursive function. After that, only it will calculate the result. Here the base case is 0. That means if the number is 0 return 1 and break the function call chain. Cause, 0! = 1 . Then it will multiply each return of each function call with the previous return value. Finally, we get the output.

7.Find The First N Fibonacci number using Recursion

In the Fibonacci series, a number is the summation of the previous 2 numbers of the series. The 0th and the 1st number of the series are 0 and 1. You have to find the first N numbers of this series by using recursion.

function fibonacci(n){
if(n === 0){
return [0];
}
else if(n === 1){
return [0, 1];
}
else{
let fiboArray = fibonacci(n - 1);
let nextNumber = fiboArray[n - 1] + fiboArray[n - 2];
fiboArray.push(nextNumber);
return fiboArray;
}
}
const series = fibonacci(10);console.log(series); //output:[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

There is 2 base case. If n is 0 return an array containing the 0th element of the Fibonacci series. And if n is 1, return an array containing 0th and 1st element of the series. For the rest, this recursive function calls itself and it calculates the next number of that series. Then it stores the result in an array by simply pushing this new number. Thus this function will call itself starting from the N and stopped calling at base case N = 0. Calling goes from top to down in recursion. But return bubbled up from down to Up.

8.Find Leap Year

A year is a leap year if it is divisible by 400 or if it is divisible by 4 but not divisible by 100. This is the condition that satisfied the condition of a leap year. Let’s see an example.

var year = 2000;if(year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)){
console.log("Leap Year');
}
else{
console.log('Not Leap Year');
}

--

--