JavaScript Common Interview Questions and Solutions Coding Examples

JavaScript Common Interview Questions and Solutions Coding Examples

Closure 2
forEach Array 3
JavaScript Map function 3
JavaScript Filter 4
Check if number is an integer 5
Is the string a palindrome 6
Reverse a String 6
Find the largest number in an array 8
Check Object Property 8
Common Elements in two Arrays 9

Closure
What is closure in JavaScript and give an example of its usage?

A closure is a function that has access to variables in its outer scope, even after the outer function has returned.

Example:
// outer function
function outerFunction(x) {

// inner function
return function innerFunction(y) {
return x + y;
}
}

const add5 = outerFunction(5);
console.log(add5(3)); // 8

Explanation: In the above example, the innerFunction has access to the x variable of the outerFunction, even after the outerFunction has returned. By assigning the return value of outerFunction(5) to the add5 constant, we are able to create a closure that adds 5 to its input.

forEach Array
How would you implement the forEach function for an array?

Array.prototype.myForEach = function(callback) {
for (let i = 0; i < this.length; i++) {
callback(this[i], i, this);
}
};

const arr = [1, 2, 3];
arr.myForEach(function(element, index, array) {
console.log(element, index, array);
});

Explanation: The myForEach function is a custom implementation of the built-in forEach method for arrays. It takes a callback function as a parameter, which it invokes for each element in the array. The callback function takes three parameters: the current element, the index of the current element, and the entire array.

JavaScript Map function
How would you implement the map function for an array?

Array.prototype.myMap = function(callback) {
const result = [];
for (let i = 0; i < this.length; i++) {
result.push(callback(this[i], i, this));
}
return result;
};

const arr = [1, 2, 3];
const doubled = arr.myMap(function(element, index, array) {
return element * 2;
});
console.log(doubled); // [2, 4, 6]

Explanation: The myMap function is a custom implementation of the built-in map method for arrays. It takes a callback function as a parameter, which it invokes for each element in the array. The callback function takes three parameters: the current element, the index of the current element, and the entire array. The myMap function returns a new array containing the results of the callback function applied to each element in the original array.

JavaScript Filter
How would you implement the filter function for an array?

Array.prototype.myFilter = function(callback) {
const result = [];
for (let i = 0; i < this.length; i++) {
if (callback(this[i], i, this)) {
result.push(this[i]);
}
}
return result;
};

const arr = [1, 2, 3, 4, 5];
const evens = arr.myFilter(function(element, index, array) {
return element % 2 === 0;
});
console.log(evens); // [2, 4]

Explanation: The myFilter function is a custom implementation of the built-in filter method for arrays. It takes a callback function as a parameter, which it invokes for each element in the array. The callback function takes three parameters: the current element, the index of the current element, and the entire array. If the callback function returns true for a given element, that element is included in the resulting array.

Check if number is an integer
How would you check if a number is an integer?

function isInteger(num) {
return typeof num === ‘number’ && Number.isInteger(num);
}

console.log(isInteger(5)); // true
console.log(isInteger(5.1)); // false

Explanation: The isInteger function takes a number as an argument and returns a boolean indicating whether or not it is an integer. It does so by checking the type of the input and using the built-in Number.isInteger method to determine if it is an integer.

Is the string a palindrome

How would you check if a given string is a palindrome?

function isPalindrome(str) {
return str === str.split(”).reverse().join(”);
}

console.log(isPalindrome(‘racecar’)); // true
console.log(isPalindrome(‘hello’)); // false

Explanation: The isPalindrome function takes a string as an argument and returns a boolean indicating whether or not it is a palindrome. It does so by splitting the string into an array of characters, reversing the array, and then joining the characters back into a string. If the resulting string is equal to the original input string, then it is a palindrome.

Reverse a String

How would you reverse a string in place (without creating a new string)?

function reverseStringInPlace(str) {
let left = 0;
let right = str.length – 1;
str = str.split(”);

while (left < right) {
const temp = str[left];
str[left] = str[right];
str[right] = temp;

sql
Copy code
left++;
right–;
}

return str.join(”);
}

console.log(reverseStringInPlace(‘hello’)); // ‘olleh’

Explanation: The reverseStringInPlace function takes a string as an argument and reverses it in place (without creating a new string). It does so by using two pointers (left and right) to keep track of the first and last characters of the string. It then swaps the characters at these positions and moves the pointers towards the center of the string until they meet in the middle. Finally, it returns the reversed string by joining the characters in the array back into a string.

Find the largest number in an array

How would you find the largest number in an array?
function findLargest(arr) {
let largest = -Infinity;
for (let i = 0; i < arr.length; i++) { if (arr[i] > largest) {
largest = arr[i];
}
}
return largest;
}

console.log(findLargest([3, 5, 2, 8, 1])); // 8

Explanation: The findLargest function takes an array of numbers as an argument and returns the largest number in the array. It does so by initializing a variable largest to a very small number, and then iterating over the array using a for loop. For each iteration, it checks if the current element is greater than largest, and if so, updates largest with the current element. Finally, it returns the largest number.

Check Object Property

How would you check if an object has a property?

function hasProperty(obj, prop) {
return obj.hasOwnProperty(prop);
}

const obj = { name: ‘John’, age: 30 };
console.log(hasProperty(obj, ‘name’)); // true
console.log(hasProperty(obj, ’email’)); // false

Explanation: The hasProperty function takes an object and a property name as arguments and returns a boolean indicating whether the object has the property. It does so by using the built-in hasOwnProperty method on the object, which returns true if the object has the specified property, and false otherwise.

Common Elements in two Arrays
How would you find the common elements between two arrays?

function findCommonElements(arr1, arr2) {
return arr1.filter(el => arr2.includes(el));
}

console.log(findCommonElements([1, 2, 3], [2, 3, 4])); // [2, 3]

Explanation: The findCommonElements function takes two arrays as arguments and returns an array of the common elements between them. It does so by using the built-in filter method on the first array, passing a callback function that uses the built-in includes method to check if the current element exists in the second array. If it does, the element is included in the resulting array.