3 Coding Challenges For Junior JavaScript Developer Interviews

Written by michaelpautov | Published 2021/11/03
Tech Story Tags: javascript | interview | junior-developer | recursion | palindrome | technical-interviews | array-duplicates | factorial

TLDRReact Native is a solid JavaScript cross-platform application development framework. It is, without a doubt, one of the most advantageous technologies for developing a chat application. In this tutorial, you will learn how to use React Native, Firebase, and Expo-CLI to create a private messaging chat app using the power of React Native. This tutorial utilizes the following technologies: React Native and Firebase. Firebase provides support for authentication for different authentication providers, including Social Auth numbers, as well as standard email and phone numbers.via the TL;DR App

JavaScript and interviews

JavaScript came out 25 years ago. Since then it has changed a lot. Today, it is one of the most popular programming languages in the world and certainly, the most popular language for web development. It is not wrong to say that JavaScript is the primary language for web development.
Because of its popularity and presence on the top level for 25 years, JavaScript interviews are not easy to clear. Actually, the JavaScript interviews can be really tough. As mentioned, there is a lot in this programming language.
Coding challenges are an important part of JavaScript interviews irrespective of the experience of the candidate. In this article, we will list the top 3 coding challenges for junior developers that are generally asked in JavaScript interviews.

Remove Duplicates From an Array

Removing duplicates from an array is one of the commonly asked coding questions in a JavaScript interview. In this question, the interview may give an array that consists of multiple values and some of them occur more than once. The task is to remove those duplicate values. For example, the following can be the input.
let arr = [1, 2, 2, 3, 2, 3, 4, 5, 4, 1, 4, 5]
The output after removing the duplicates should be:
let arr = [1, 2, 3, 4, 5]
This coding challenge can be solved in multiple ways. The simplest way is to use sets.
let arr = [1, 2, 2, 3, 2, 3, 4, 5, 4, 1, 4, 5];
let output = [...new Set(arr)];
console.log(output); //[1, 2, 3, 4, 5]
Though, there is another (and recommended) way, which is by using the filter() method along with the indexOf() method.
let arr = [1, 2, 2, 3, 2, 3, 4, 5, 4, 1, 4, 5];
let result = arr.filter((item, index) => {
  return arr.indexOf(item) == index;
});
console.log(result); //[1, 2, 3, 4, 5]
The trick here is the indexOf() method. This method returns the index of the item but the index returned is always of the first occurrence of that item. For example, 1 occurs at the 0th index as well as at the 9th index. But the indexOf method will always return 0. So, it means the condition in the return statement will only match once, does not matter how many times the element occurs in the array. Thus every element is added to the “result” array only once.

Check If a String is Palindrome or Not

A string is a palindrome when its reverse is exactly equal to its original form. For example, “madam” is a palindrome string because its original and reverse are exactly the same. Similarly, “car” is not a palindrome because its reverse (“rac”) is not the same as its original value.
This is a very commonly asked coding challenge to the junior developers because it is regarding the concept of reversing values.
A simple solution to this challenge is by reversing the string and checking its equality with the original string.
For this, first, use the lowerCase() method to convert every string into one case. Then, use the split() to split the string into an array because the reverse() method can only be used on the array. When the array is reversed, use the join() method to convert it back into a string again.
Finally, check if the original and reverse strings are equal or not.
let string = "madam";
string = string.toLowerCase();
let reversedString = string.split("").reverse().join("");
console.log(reversedString); //madam
if (string === reversedString) {
  console.log(`${string} is palindrome`);
} else {
  console.log(`${string} is NOT palindrome`);
}

Find the Factorial of the Given Number

A factorial of a number is easy to find on paper. For example, the factorial of 5 is 120. It is calculated by multiplying 5 with 4 and its result with 3 and its result with 2, and its result with 1. But writing a code to find the factorial can be tricky.
A simple way to complete this coding challenge is by using the while loop.
let numberber = 5;
let sum = number;
while (number > 1) {
  number = number - 1;
  sum = sum * number;
}

console.log(sum); //120
Here, the given number is first decremented and then multiplied with its original value. This continues until the number becomes 1. The result is accurate but it is recommended not to use this logic because the interviewer’s motive behind this question is to check the recursion knowledge of the candidate.
The factorial coding challenge is expected to be solved using recursion.
function recursiveFunction(number) {
  if (number == 1) {
    return 1;
  }

  return number * recursiveFunction(number - 1);
}

console.log(recursiveFunction(5));
Here, a recursive function is used to find the factorial of the given number. The code runs until the number becomes 1 because the value of the number is decreasing with every recursive call. In the end, the value returned by the function is the factorial of the given number.

Wrapping it up

These are the three most commonly asked coding challenges in a JavaScript interview for a junior developer. Remember, the interviewer wants to see how you think about the logic. The interviewer wants you to solve the challenge as efficiently as possible. The main goal is to check your programming knowledge and skills. For example, the third challenge in this article could be solved by using a while loop, which is easy but using recursion shows you have better knowledge and skill.

Written by michaelpautov | Software Engineer
Published by HackerNoon on 2021/11/03