Posts

Showing posts with the label JavaScript Arrays

Search Atharv Gyan

NEW

Are Decentralized Stablecoins the Future of DeFi?

View on YouTube

View on Spotify

Catch this episode and more on Spotify Stay updated with the latest in tech and climate solutions.

JavaScript interview questions & answers with code

Image
Top JavaScript Interview Questions and Expert Answers with Code Examples 1. Flatten a Nested Object Question : Write a function to flatten a nested JavaScript object. Code : function flattenObject(obj, prefix = '', res = {}) {   for (let key in obj) {     const newKey = prefix ? `${prefix}.${key}` : key;     if (typeof obj[key] === 'object' && obj[key] !== null) {       flattenObject(obj[key], newKey, res);     } else {       res[newKey] = obj[key];     }   }   return res; } console.log(flattenObject({ a: { b: { c: 1 } }, d: 2 })); // Output: { 'a.b.c': 1, d: 2 } 2. Find Duplicates in an Array Question : Write a function to find duplicate values in an array. Code : function findDuplicates(arr) {   const counts = {};   return arr.filter(item => counts[item] ? true : (counts[item] = 1, false)); } console.log(findDuplicates([1, 2, 2, 3, 4, 4, 5])); // Output: [2, 4] 3. Implement deboun...
🔍 DevTools is open. Please close it to continue reading.
Atharv Gyan Splash Screen