Top 10 JavaScript Interview Problems and Solutions You Must Prepare – Part 1
Top 10 JavaScript Interview Problems and Solutions You Must Prepare
- Write a function to calculate the sum of all elements in an array.
function sumArray(arr) {
return arr.reduce((acc, num) => acc + num, 0);
}
const result = sumArray([1, 2, 3, 4, 5]);
console.log(result); // Expected output: 15
- Write a function to remove all falsy values (e.g.,
false
,null
,0
,""
,undefined
, andNaN
) from an array.
function removeFalsy(arr) {
return arr.filter(Boolean);
}
const result = removeFalsy([1, 0, true, false, '', 'hello', null, undefined]);
console.log(result); // Expected output: [1, true, 'hello']
- Write a function to convert the first letter of each word in a sentence to uppercase.
function titleCase(sentence) {
return sentence.replace(/\b\w/g, char => char.toUpperCase());
}
const result = titleCase('this is a sample sentence');
console.log(result); // Expected output: 'This Is A Sample Sentence'
- Write a function to remove duplicate elements from an array.
function removeDuplicates(arr) {
return [...new Set(arr)];
}
const result = removeDuplicates([1, 2, 2, 3, 4, 4, 5]);
console.log(result); // Expected output: [1, 2, 3, 4, 5]
- Write a function to check if a given number is prime.
function isPrime(num) {
if (num < 2) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
const result = isPrime(7);
console.log(result); // Expected output: true
- Write a function to find the maximum and minimum values in an array.
function findMinMax(arr) {
const max = Math.max(...arr);
const min = Math.min(...arr);
return { max, min };
}
const result = findMinMax([3, 1, 7, 4, 2, 9]);
console.log(result); // Expected output: { max: 9, min: 1 }
With out using math function
function findMaxMin(arr) {
// Check if the array is not empty
if (arr.length === 0) {
return { max: undefined, min: undefined };
}
// Initialize max and min with the first element of the array
let max = arr[0];
let min = arr[0];
// Iterate through the array starting from the second element
for (let i = 1; i < arr.length; i++) {
// Update max if the current element is greater
if (arr[i] > max) {
max = arr[i];
}
// Update min if the current element is smaller
if (arr[i] < min) {
min = arr[i];
}
}
// Return the result as an object
return { max, min };
}
// Example usage
const numbers = [5, 2, 8, 1, 7, 3];
const result = findMaxMin(numbers);
console.log(result); // Output: { max: 8, min: 1 }
- Write a function to capitalize the first letter of each word in a sentence.
function capitalizeWords(sentence) {
return sentence.replace(/\b\w/g, char => char.toUpperCase());
}
const result = capitalizeWords('hello world');
console.log(result); // Expected output: 'Hello World'
- Write a function to check if two strings are anagrams.
function areAnagrams(str1, str2) {
const sortedStr1 = str1.split('').sort().join('');
const sortedStr2 = str2.split('').sort().join('');
return sortedStr1 === sortedStr2;
}
const result = areAnagrams('listen', 'silent');
console.log(result); // Expected output: true
- Write a function to rotate the elements of an array to the right by a given number of positions.
function rotateRight(arr, positions) {
const n = arr.length;
const rotated = [...arr.slice(n - positions), ...arr.slice(0, n - positions)];
return rotated;
}
const result = rotateRight([1, 2, 3, 4, 5], 2);
console.log(result); // Expected output: [4, 5, 1, 2, 3]
- Write a function to flatten a nested array.
function flattenArray(arr) {
return arr.flat(Infinity);
}
const result = flattenArray([1, [2, [3, 4], 5], 6]);
console.log(result); // Expected output: [1, 2, 3, 4, 5, 6]
Mastering JavaScript: Tackling the Top 10 Interview Challenges with Expert Solutions
Essential JavaScript Interview Questions: A Comprehensive Guide to the Top 10 Problems and Solutions
Elevate Your Coding Game: Top 10 JavaScript Interview Problems Demystified
Crack the Code: Navigating the Top 10 JavaScript Interview Challenges Like a Pro
JavaScript Mastery Unleashed: Your Ultimate Guide to Conquering Interview Problems and Solutions
Unraveling JavaScript Interview Mysteries: Top 10 Problems Solved
Beyond Basics: A Deep Dive into the Top 10 JavaScript Interview Questions and Answers
JavaScript Wizards’ Handbook: Tackling the Toughest Interview Problems Head-On
Decode the Challenges: Top 10 JavaScript Interview Problems and Expert Solutions Unveiled
Rise to the Challenge: Mastering JavaScript Interview Problems with Precision and Ease