JavaScript Programmatic Interview Questions

Abhay Ahire
2 min readJan 25, 2022

In this article, we are going to take a look at commonly asked JavaScript programmatic questions during interview to test your skills.
So let’s start with some simple tricks playing around strings

Replace first occurrence of char in a string

In this case, you need to replace just first occurrence of a character with given character.
e.g. “Abhay is an author at medium”. Replace ‘a’ with ‘x’.

Notice replace function is case sensitive, so it skipped first ‘A’ and replaced second.

Replace All occurrence of character in a string

In this case, you need to replace all occurrence of a character with given character.
e.g. “Abhay is an author at medium”. Replace all ‘a’ with ‘x’.

Here we need to use regex to select all occurrences if required.

Swap the two variables content without a third variable

This is very interesting question to exchange contents of two vars without need of third variable. JS comes with feature of destructoring to achieve same.

Calculate the sum of digits of the passed integer

In this case we need to calculate sum of every digit in a number.

Notice we have converted number to string and then converted back it to array to use a higher order function reduce, but before that we are using map function to convert elements back to number.
This style of code is called method chaining. If possible format it as above for good code readability.

Calculate factorial (n!) of the passed integer

We need to calculate nth factorial here.

Here we are using recursive strategy to calculate factorial. Because we are calling getFactorial function inside getFactorial function. In recursive strategy, break point is key to end the loop, in our case number <= 1 is the break point.

How to empty an Array in JavaScript?

There are few ways to empty an array, we are going to see most easy way of them

Write down a function to find unique elements in array

This is most commonly asked/used case for arrays to remove duplicates from an array.
Let’s look at simple modern ES6 way to do this.

Yes, that’s it, Set removes duplicates automatically and to convert it back to array, you need to call Array.from. But what if you want to write your own function and do some modification. Let’s do that as well.

Using a higher order function filter and using indexOf we can remove duplicates. You can further modify it as per your needs.

--

--

Abhay Ahire

Full stack Dev with Angular, Java , Node and Devops Engg AWS