Javascript Interview Questions
Question1 : Merge Two Arrays in Javascript
Here, we will simply use the spread operator to spread the data into
the entire new array.
function mergeArray(arr1, arr2) {
const merge = [...arr1, ...arr2]
console.log(merge)
}
const arr1 = [1, 2, 3]
const arr2 = [4, 5, 6]
mergeArray(arr1, arr2)
output : [ 1, 2, 3, 4, 5, 6 ]
Question2 : Merge Array of 2 Different Types
function mergeArray(obj1, obj2) {
if (Array.isArray(obj1) && Array.isArray(obj2)) {
const merge = […obj1, …obj2]
console.log(merge)
}
else if (typeof (obj1) === “object” && typeof (obj2) === “object” && !Array.isArray(obj1) && !Array.isArray(obj2)) {
const merge = { …obj1, …obj2 }
console.log(merge)
} else {
console.log(“Different Data Type Cannot Be Merged”)
}
}
const arr1 = [1,2,3]
const arr2 = [4,5,6]
const obj1= {a:1,b:2,c:3}
const obj2 = {d:4,e:5,f:6}
mergeArray(arr1,arr2)
Output : [ 1, 2, 3, 4, 5, 6 ]
mergeArray(obj1,obj2)
Output : { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }
mergeArray(arr1,obj2)
Output : Different Data Type Cannot Be Merged
Question3 : Find the Max and Min Value from an Array.
function findMinandMax(arr) {
const min = Math.min(...arr)
const max = Math.max(...arr)
console.log("Min Value", min)
console.log("Max Value", max)
}
You can also pass the n number of the arguments using (...rest) operator
function findMinandMax(arr) {
const min = Math.min(...arr)
const max = Math.max(...arr)
console.log("Min Value", min)
console.log("Max Value", max)
}
arr1 = [1, 2, 3, 4, 5]
findMinandMax(arr1)
Output - Min Value : 1
Output - Max Value : 5
Question4 : Rotate an array k times.
function RotateArrayKTimes(arr, k) {
for (let i = 1; i <= k; i++) {
// Remove Element from the last index
const lastItem = arr.pop()
// Adding LastItem to the strting of an array
arr.unshift(lastItem)
}
console.log(arr)
}
arr1 = [1, 2, 3, 4, 5]
RotateArrayKTimes(arr1,3)
Output = [ 3, 4, 5, 1, 2 ]
Question 5 : Write Javascript function to Remove Duplicate Strigs from an Array.
function RemoveDuplicateCharacters(string) {
const uniqueCharactersSet = new Set(string)
const uniqueCharacterArray = [...uniqueCharactersSet]
const finalString = uniqueCharacterArray.join("")
console.log(finalString)
}
RemoveDuplicateCharacters("Programming")
Output = Progamin
Question 6: Check if the Arrays are Equal or Not:
function checkIfArraysAreEqual(arr1, arr2) {
if (arr1.length != arr2.length) {
console.log("Length Mismatched, Array is not Equal")
return;
}
const sortedArr1 = arr1.sort()
const sortedArr2 = arr2.sort()
for (let i = 0; i < sortedArr1.length; i++) {
if (sortedArr1[i] != sortedArr2[i]) {
console.log("Arrays are not equal")
return;
}
}
console.log("Arrays are Equal")
}
const arr1 = [1, 2, 3]
const arr2 = [3, 2, 1]
checkIfArraysAreEqual(arr1, arr2)
Output = Arrays are Equal
Question 7 : Remove the occurances of the target in the given array.
function removeOccurances(arr, target) {
const resultArray = arr.filter((element)=> element != target)
console.log(resultArray)
}
arr1=[1,2,3,4,4,4,5]
removeOccurances(arr1,4)
output = [ 1, 2, 3, 5 ]
Question 8 : find the common Elements From Arrays.
function findCommonElementBetweenTwoArrays(arr1, arr2) {
const commonElementArray = arr1.filter((element) => arr2.includes(element))
console.log(commonElementArray)
}
const arr1 = [1, 2, 3, 4]
const arr2 = [3, 4, 5, 6]
findCommonElementBetweenTwoArrays(arr1,arr2)
Output = [ 3, 4 ]
Question 9 : find the square of all the elements in the array.
function findSquare(arr){
const squaredArray = arr.map((ele)=> ele ** 2)
console.log(squaredArray)
}
const arr1 = [3,4,5,6]
findSquare(arr1)
output = [ 9, 16, 25, 36 ]
Question 10 : Calculate the product of all elements of an array
function calculateProductOfAllNumbers(arr) {
const result = arr.reduce((ele,acc)=> acc * ele,1)
console.log(result)
}
const arr = [1, 2, 3, 4, 5]
calculateProductOfNumbers(arr)
output = 120
Question11 : Write a fnction to swap the values using temp variable
function swapValues(a, b) {
console.log('Before Swapping a was', a)
console.log('Before Swapping a was', b)
let temp;
temp = a;
a = b;
b = temp;
console.log("After Swaping a is", a)
console.log("After Swapping b is", b)
}
swapValues(10,15)
Before Swapping a was 10
Before Swapping a was 15
After Swapping a is 15
After Swapping b is 10
Question 12 : Write a Javascript function to sort amn array in the decening order without using inbuilt method.
function sortArrayDecending(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = i; j < arr.length; j++) {
if (arr[i] < arr[j]) {
let temp;
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp;
}
}
}
console.log(arr)
}
output = [ 5, 4, 3, 2, 1 ]
Question 13 : Sort an array in ascening order using any inbuilt sorting method.
function sortArrayDecending(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = i; j < arr.length; j++) {
if (arr[i] > arr[j]) {
let temp;
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp;
}
}
}
console.log(arr)
}
const arr1 = [1, 2, 3, 4, 5]
sortArrayDecending(arr1)
output = [1, 2, 3, 4, 5]
Question 14 : Calculate the sum of all the elements in an array.
function calculateSumofNumbers(arr){
const sum = arr.reduce((ele,acc)=> ele + acc , 0)
console.log(sum)
}
arr1 = [1,2,3,4,5]
calculateSumofNumbers(arr1)
output = 15
Question 15: find all the minimum values in sliding subrray.
function findMinimumInSubArray(arr, k) {
let result = []
for (let i = 0; i <= arr.length - k; i++) {
let temp = []
for (let j = i; j < i + k; j++) {
temp.push(arr[j])
}
const minVal = Math.min(...temp)
result.push(minVal)
}
console.log(result)
}
arr1 = [1, 2, 3, -3, -5, 6]
findMinimumInSubArray(arr1,3)
output = [ 1, -3, -5, -5 ]
Question 16 : find the first negative number from the sliding subarray.
function firstNegativeNumberSlidingSubArray(arr, k) {
let result = []
for (let i = 0; i <= arr.length - k; i++) {
for (let j = i; j < i + k; j++) {
if (arr[j] < 0) {
result.push(arr[j])
break;
}
}
}
console.log(result)
}
arr1=[-2,3,4,-5,6,-7]
firstNegativeNumberSlidingSubArray(arr1,3)
output = [ -2, -5, -5, -5 ]