0% found this document useful (0 votes)
52 views50 pages

50 JavaScript Coding Problems With Solutions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views50 pages

50 JavaScript Coding Problems With Solutions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Frontend Developer Coding Questions

1. Reverse a String
function reverseString(str) {
return str.split("").reverse().join("");
}
Frontend Developer Coding Questions

2. Palindrome Check
function isPalindrome(str) {
return str === str.split("").reverse().join("");
}
Frontend Developer Coding Questions

3. FizzBuzz
for(let i=1; i<=100; i++) {
let res = "";
if(i%3===0) res += "Fizz";
if(i%5===0) res += "Buzz";
console.log(res || i);
}
Frontend Developer Coding Questions

4. Count Vowels in a String


function countVowels(str) {
return (str.match(/[aeiou]/gi) || []).length;
}
Frontend Developer Coding Questions

5. Find Duplicate Elements


function findDuplicates(arr) {
const seen = new Set();
const dups = new Set();
for (let i of arr) {
if (seen.has(i)) dups.add(i);
seen.add(i);
}
return [...dups];
}
Frontend Developer Coding Questions

6. Factorial of a Number
function factorial(n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
Frontend Developer Coding Questions

7. Check Prime Number


function isPrime(n) {
if (n <= 1) return false;
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i === 0) return false;
}
return true;
}
Frontend Developer Coding Questions

8. Fibonacci Sequence
function fibonacci(n) {
const seq = [0, 1];
for(let i=2; i<n; i++) {
seq[i] = seq[i-1] + seq[i-2];
}
return seq;
}
Frontend Developer Coding Questions

9. Anagram Check
function isAnagram(str1, str2) {
return str1.split('').sort().join('') === str2.split('').sort().join('');
}
Frontend Developer Coding Questions

10. Capitalize First Letter


function capitalizeWords(str) {
return str.split(" ").map(word => word[0].toUpperCase() + word.slice(1)).join(" ");
}
Frontend Developer Coding Questions

11. Remove Duplicates from Array


function removeDuplicates(arr) {
return [...new Set(arr)];
}
Frontend Developer Coding Questions

12. Find Max Element in Array


function findMax(arr) {
return Math.max(...arr);
}
Frontend Developer Coding Questions

13. Find Min Element in Array


function findMin(arr) {
return Math.min(...arr);
}
Frontend Developer Coding Questions

14. Sum of Array Elements


function arraySum(arr) {
return arr.reduce((a, b) => a + b, 0);
}
Frontend Developer Coding Questions

15. Find Intersection of Two Arrays


function intersection(arr1, arr2) {
return arr1.filter(value => arr2.includes(value));
}
Frontend Developer Coding Questions

16. Flatten Nested Array


function flatten(arr) {
return arr.flat(Infinity);
}
Frontend Developer Coding Questions

17. Convert String to Array


function stringToArray(str) {
return str.split(" ");
}
Frontend Developer Coding Questions

18. Count Occurrences of Character


function countChar(str, char) {
return str.split(char).length - 1;
}
Frontend Developer Coding Questions

19. Check if Array is Sorted


function isSorted(arr) {
return arr.every((val, i, a) => i === 0 || a[i - 1] <= val);
}
Frontend Developer Coding Questions

20. Merge Two Sorted Arrays


function mergeSorted(a, b) {
return [...a, ...b].sort((x, y) => x - y);
}
Frontend Developer Coding Questions

21. Binary Search


function binarySearch(arr, target) {
let left = 0, right = arr.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === target) return mid;
else if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
Frontend Developer Coding Questions

22. Check if Object is Empty


function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
Frontend Developer Coding Questions

23. Debounce Function


function debounce(fn, delay) {
let timeout;
return function(...args) {
clearTimeout(timeout);
timeout = setTimeout(() => fn.apply(this, args), delay);
};
}
Frontend Developer Coding Questions

24. Throttle Function


function throttle(fn, limit) {
let inThrottle;
return function(...args) {
if (!inThrottle) {
fn.apply(this, args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
}
Frontend Developer Coding Questions

25. Deep Clone Object


function deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
Frontend Developer Coding Questions

26. Swap Two Variables


let a = 1, b = 2;
[a, b] = [b, a];
Frontend Developer Coding Questions

27. Generate Random Number in Range


function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
Frontend Developer Coding Questions

28. Validate Email Format


function isValidEmail(email) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}
Frontend Developer Coding Questions

29. Generate Random Hex Color


function getRandomColor() {
return '#' + Math.floor(Math.random()*16777215).toString(16);
}
Frontend Developer Coding Questions

30. Convert RGB to Hex


function rgbToHex(r, g, b) {
return "#" + [r, g, b].map(x => x.toString(16).padStart(2, '0')).join("");
}
Frontend Developer Coding Questions

31. String Compression


function compressString(str) {
return str.replace(/(.)\1+/g, (m, c) => c + m.length);
}
Frontend Developer Coding Questions

32. Longest Word in Sentence


function longestWord(str) {
return str.split(" ").sort((a,b) => b.length - a.length)[0];
}
Frontend Developer Coding Questions

33. Check Power of Two


function isPowerOfTwo(n) {
return (n > 0) && ((n & (n - 1)) === 0);
}
Frontend Developer Coding Questions

34. Convert to CamelCase


function toCamelCase(str) {
return str.replace(/[-_](.)/g, (_, c) => c.toUpperCase());
}
Frontend Developer Coding Questions

35. Count Words in Sentence


function countWords(str) {
return str.trim().split(/\s+/).length;
}
Frontend Developer Coding Questions

36. Check Balanced Parentheses


function isBalanced(str) {
let stack = [];
for (let ch of str) {
if (ch === '(') stack.push(ch);
else if (ch === ')') {
if (!stack.length) return false;
stack.pop();
}
}
return stack.length === 0;
}
Frontend Developer Coding Questions

37. Replace All Occurrences in String


function replaceAll(str, find, replace) {
return str.split(find).join(replace);
}
Frontend Developer Coding Questions

38. Sort Array by Length of Strings


function sortByLength(arr) {
return arr.sort((a, b) => a.length - b.length);
}
Frontend Developer Coding Questions

39. Remove Falsy Values


function removeFalsy(arr) {
return arr.filter(Boolean);
}
Frontend Developer Coding Questions

40. Generate Fibonacci using Recursion


function fib(n) {
if (n <= 1) return n;
return fib(n-1) + fib(n-2);
}
Frontend Developer Coding Questions

41. Sum Digits of a Number


function sumDigits(n) {
return n.toString().split('').reduce((a, b) => a + +b, 0);
}
Frontend Developer Coding Questions

42. Convert Number to Binary


function toBinary(n) {
return n.toString(2);
}
Frontend Developer Coding Questions

43. Convert Binary to Decimal


function toDecimal(bin) {
return parseInt(bin, 2);
}
Frontend Developer Coding Questions

44. GCD of Two Numbers


function gcd(a, b) {
return b === 0 ? a : gcd(b, a % b);
}
Frontend Developer Coding Questions

45. LCM of Two Numbers


function lcm(a, b) {
return (a * b) / gcd(a, b);
}
Frontend Developer Coding Questions

46. Find Missing Number in Array


function findMissing(arr) {
let n = arr.length + 1;
let sum = (n * (n + 1)) / 2;
return sum - arr.reduce((a, b) => a + b, 0);
}
Frontend Developer Coding Questions

47. Get Unique Values from Array of Objects


function getUnique(arr, key) {
const seen = new Set();
return arr.filter(item => {
if (seen.has(item[key])) return false;
seen.add(item[key]);
return true;
});
}
Frontend Developer Coding Questions

48. Check Palindrome Number


function isPalindromeNumber(num) {
return num.toString() === num.toString().split('').reverse().join('');
}
Frontend Developer Coding Questions

49. Find All Pairs That Sum to Target


function findPairs(arr, target) {
const pairs = [];
const set = new Set();
for (let num of arr) {
if (set.has(target - num)) {
pairs.push([num, target - num]);
}
set.add(num);
}
return pairs;
}
Frontend Developer Coding Questions

50. Sort Array of Objects by Key


function sortByKey(arr, key) {
return arr.sort((a, b) => a[key] - b[key]);
}

You might also like