🚀 Day 26/30 – Flatten a Multi-Dimensional Array (Depth Controlled) 🧠 📌 Problem Given: A multi-dimensional array arr An integer depth n Return a flattened version of arr. Rules: The array may contain integers or nested arrays Only flatten elements if current depth < n Depth of first-level elements = 0 Do NOT use Array.flat() 🧠 Example 1 arr = [1, [2, [3, 4]], 5] n = 1 Output: [1, 2, [3, 4], 5] 🧠 Example 2 arr = [1, [2, [3, 4]], 5] n = 2 Output: [1, 2, 3, 4, 5] 💡 JavaScript Solution (Recursive Approach) var flat = function(arr, n) { const result = []; function flatten(current, depth) { for (let item of current) { if (Array.isArray(item) && depth < n) { flatten(item, depth + 1); } else { result.push(item); } } } flatten(arr, 0); return result; }; 🔎 Why This Works We track depth Only flatten when depth < n Recursion handles unlimited nesting Preserves element order Time Complexity: O(total elements) Space Complexity: O(total elements + recursion stack) 🧠 What This Teaches ✅ Recursion fundamentals ✅ Depth tracking ✅ Tree-like traversal ✅ Controlling execution scope ✅ How Array.flat() actually works internally ⚡ Real-World Use Cases Normalizing API responses Parsing nested JSON Flattening comment threads Handling deeply nested UI state Transforming hierarchical data #JavaScript #30DaysOfJavaScript #CodingChallenge #Recursion #DataStructures #Algorithms #FrontendDevelopment #WebDevelopment #LearnToCode #SoftwareEngineering #ProblemSolving #TechCommunity #InterviewPrep #100DaysOfCode #JSDeveloper JavaScript flatten array without flat Recursive array flattening JS Flatten array with depth JavaScript Implement Array.flat manually JavaScript recursion interview question Multi-dimensional array flatten JS Depth controlled flattening JavaScript Advanced JavaScript problem solving
Flatten Multi-Dimensional Array with Depth Control in JavaScript
More Relevant Posts
-
Hello everyone, Here is my blog on JavaScript Array Methods with examples. I tried to explain the concepts in a simple way. Please read it and share your valuable feedback. It will help me improve my learning and writing. 🙌 https://lnkd.in/gs8aJ2bS Chai Aur Code, Hitesh Choudhary Piyush Garg Akash Kadlag Jay Kadlag #array #js #chaiaurcode #writing #learning
To view or add a comment, sign in
-
Why do people say “Everything in JS is an object” ?? Does that mean primitive datatypes like string, number, and boolean are objects too? But wait… const str = "hello" console.log(typeof str) // "string" It clearly says "string" — not "object". So where is the object? The answer is hidden in proto. If you check: str.proto You will see an object that contains all the methods we use on strings: • at() • charAt() • includes() • indexOf() • toUpperCase() • length and many more… That object is String.prototype. So what’s happening behind the scenes? When you use: "hello".toUpperCase() JavaScript temporarily wraps the primitive string into an object like this: "hello" → new String("hello") This wrapper object is linked to String.prototype, and that’s how you can access string methods. Then why is typeof "hello" not "object"? Because the actual value is still a primitive. JavaScript just gives it temporary access to an object through the prototype chain. This mechanism is called autoboxing. So when people say “Everything in JS is an object”, what they really mean is: • Objects inherit from Object.prototype • Arrays inherit from Array.prototype • Functions inherit from Function.prototype • Even primitives are linked to their wrapper prototypes • Everything (except null and undefined) connects to an object internally JavaScript is prototype-based. Check out the screenshots I attached — you’ll see the prototype object containing all string methods. "So interestingly, this also allows us to modify the methods and properties of a datatype. Because all strings are linked to String.prototype, if you change something inside that prototype, it affects every string in your application." If you want to know more about Objects in JavaScript, read this blog: https://lnkd.in/dmMDv939 #javascript #javascriptObject #chaicode #chaiaurcode
To view or add a comment, sign in
-
-
🚨 I finally stopped forcing everything into plain Objects & Arrays in JavaScript… and my code became cleaner, faster, and completely leak-free overnight. If you’re still dealing with: 1️⃣ String-only keys 2️⃣ Duplicate values messing up your logic 3️⃣ Manual .length hacks 4️⃣ Prototype pollution or sneaky memory leaks …then this guide is going to change how you write JS forever. I created a complete, no-fluff 13-page PDF that breaks down the 4 powerful data structures most developers overlook: 1️⃣ Map → Any type of key + guaranteed order + .size built-in 2️⃣ Set → Unique values only + blazing-fast lookups 3️⃣ WeakMap → Private data without memory leaks 4️⃣ WeakSet → Safe object tracking that auto-cleans itself What’s inside: ✅ Crystal-clear explanations with real code examples ✅ Side-by-side comparison: Object vs Map | Array vs Set ✅ Exact “When & Why” scenarios (interview favorite) ✅ Mathematical Set operations (Union, Intersection, etc.) ✅ 5 practical coding tasks with full solutions ✅ Top 6 interview questions & answers 📥 The full PDF is attached — download it right now, open your editor, and start using these today. You’ll feel the difference in minutes. Save this post. Share it with one developer friend who’s still stuck with basic objects/arrays. Follow me for more practical JavaScript deep-dives, real-world tips, and ready-to-use resources that actually level up your code. Full notes + all code examples are on my GitHub Let’s keep growing together! 💪 #JavaScript #WebDevelopment #Frontend #CodingTips #DataStructures #InterviewPrep #100DaysOfCode
To view or add a comment, sign in
-
When datasets hit a million rows, performance gaps get real. Jessica Wachtel explores how WebAssembly pulls ahead of JavaScript in heavy browser-based processing.
To view or add a comment, sign in
-
𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗦𝘆𝗺𝗯𝗼𝗹𝘀: 𝗧𝗵𝗲 "𝗦𝗲𝗰𝗿𝗲𝘁 𝗛𝗼𝗼𝗸𝘀" 𝗼𝗳 𝘁𝗵𝗲 𝗟𝗮𝗻𝗴𝘂𝗮𝗴𝗲 💎 Most developers know that object keys are strings. But did you know there is a second, "unique" way to label your data? 𝗪𝗵𝘆 𝗦𝘆𝗺𝗯𝗼𝗹𝘀 𝗮𝗿𝗲 𝗮 𝗴𝗮𝗺𝗲-𝗰𝗵𝗮𝗻𝗴𝗲𝗿 𝗳𝗼𝗿 𝘆𝗼𝘂𝗿 𝗰𝗼𝗱𝗲𝗯𝗮𝘀𝗲: 1️⃣ 𝗚𝘂𝗮𝗿𝗮𝗻𝘁𝗲𝗲𝗱 𝗨𝗻𝗶𝗾𝘂𝗲𝗻𝗲𝘀𝘀: Every Symbol() you create is one-of-a-kind. No more accidental property overrides or name collisions in large applications. 2️⃣ 𝗣𝗿𝗶𝘃𝗮𝗰𝘆 𝗯𝘆 𝗢𝗯𝘀𝗰𝘂𝗿𝗶𝘁𝘆: While not truly "private," Symbols don't show up in for...in loops or Object.keys(). They are perfect for hiding internal metadata. 3️⃣ 𝗘𝗻𝗴𝗶𝗻𝗲 𝗛𝗼𝗼𝗸𝘀: Using "Well-Known Symbols," you can actually change how your objects behave. You can tell the JS engine exactly how to convert your object to a number (Symbol.toPrimitive) or what its custom name should be in logs (Symbol.toStringTag). Understanding Symbols is like getting the keys to the engine room. It’s how the pros write code that is backwards-compatible and plays nicely with the built-in language features. Check out the full breakdown here: https://lnkd.in/epSBNb6s 🔗 𝗡𝗲𝘅𝘁 𝗨𝗽: The most requested topic in JS, 𝗔𝘀𝘆𝗻𝗰𝗵𝗿𝗼𝗻𝗼𝘂𝘀 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁. ⏳⚙️ #𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 #𝗪𝗲𝗯𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗺𝗲𝗻𝘁 #𝗣𝗿𝗼𝗴𝗿𝗮𝗺𝗺𝗶𝗻𝗴 #𝗦𝗼𝗳𝘁𝘄𝗮𝗿𝗲𝗘𝗻𝗴𝗶𝗻𝗲𝗲𝗿𝗶𝗻𝗴 #𝗖𝗼𝗱𝗶𝗻𝗴𝗧𝗶𝗽𝘀 #𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁𝗦𝘆𝗺𝗯𝗼𝗹𝘀 #𝗙𝗿𝗼𝗻𝘁𝗲𝗻𝗱𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗺𝗲𝗻𝘁 #𝗧𝗲𝗰𝗵𝗖𝗼𝗺𝗺𝘂𝗻𝗶𝘁𝘆
To view or add a comment, sign in
-
💡 𝘼 𝙎𝙢𝙖𝙡𝙡 𝙅𝙖𝙫𝙖𝙎𝙘𝙧𝙞𝙥𝙩 𝙎𝙠𝙞𝙡𝙡 𝙏𝙝𝙖𝙩 𝙎𝙖𝙫𝙚𝙨 𝙖 𝙇𝙤𝙩 𝙤𝙛 𝙏𝙞𝙢𝙚: 𝙍𝙚𝙜𝙚𝙭 After around 2 years of working in frontend development, one thing I’ve realized is that small tools can make a big difference. One of them is Regex (Regular Expressions). At first, Regex looked confusing to me: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ But once I understood the basics, it became extremely useful for working with text in web applications. 🚀 Where Regex helps in real projects ✔ Validating form inputs (email, password, phone number) ✔ Cleaning user input ✔ Extracting numbers or patterns from text ✔ Search and replace operations ⚡ Example – Check if a string contains numbers /\𝘥+/.𝘵𝘦𝘴𝘵("𝘖𝘳𝘥𝘦𝘳 𝘐𝘋: 12345") // 𝘵𝘳𝘶𝘦 \𝘥 → 𝘮𝘢𝘵𝘤𝘩𝘦𝘴 𝘥𝘪𝘨𝘪𝘵𝘴 + → 𝘰𝘯𝘦 𝘰𝘳 𝘮𝘰𝘳𝘦 𝘥𝘪𝘨𝘪𝘵𝘴 🧠 A few Regex symbols every JavaScript developer should know 1️⃣ \d → digit (0–9) 2️⃣ \w → word character 3️⃣ \s → whitespace 4️⃣ + → one or more 5️⃣ * → zero or more 6️⃣ ^ → start of string 7️⃣ $ → end of string Regex might look intimidating at first, but learning a few patterns can make text processing much easier and cleaner in JavaScript applications. Still learning, still improving 🚀 What’s a small JavaScript concept that made your development workflow easier? #JavaScript #Regex #FrontendDevelopment #WebDevelopment #Learning #CodingTips
To view or add a comment, sign in
-
Mastering JavaScript Objects — The Foundation of Everything in JS If you're learning JavaScript, objects are the single most important concept to understand deeply. Here's what every developer should know 👇 ━━━━━━━━━━━━━━━━━━━━━━ 📦 What is a JavaScript Object? An object is a collection of key-value pairs that lets you model real-world data in your code. const developer = { name: "Alice", skills: ["JS", "React"], greet() { return `Hi, I'm ${this.name}`; } }; ━━━━━━━━━━━━━━━━━━━━━━ ✅ 5 Things You Must Know About Objects: 1️⃣ Objects store data as properties (key: value) 2️⃣ Methods are just functions living inside objects 3️⃣ Use dot (.) or bracket ([]) notation to access values 4️⃣ Objects are passed by reference — not by value 5️⃣ Everything in JavaScript is (almost) an object ━━━━━━━━━━━━━━━━━━━━━━ 💡 Power Features You Should Be Using: 🔹 Destructuring → const { name, age } = user; 🔹 Spread Operator → const copy = { ...obj }; 🔹 Object.entries() → Loop key-value pairs easily 🔹 Optional Chaining → user?.address?.city ━━━━━━━━━━━━━━━━━━━━━━ Whether you're building APIs, managing state in React, or designing data models — objects are EVERYWHERE. The developers who truly understand objects write cleaner, faster, and more maintainable code. 💪 👇 Drop a comment — What's YOUR favourite object trick or method? #JavaScript #WebDevelopment #Programming #Frontend #100DaysOfCode #CodeNewbie #TechCommunity #SoftwareEngineering
To view or add a comment, sign in
-
-
New blog on function declarations vs function expressions in JavaScript. Both are ways to define functions, but they behave differently in practice, especially in terms of when they can be used and how they’re created in the code. If you’re learning JavaScript fundamentals, this might help: https://lnkd.in/gn6DtpZJ Feedback is welcome. Chai Aur Code Hitesh Choudhary Piyush Garg
To view or add a comment, sign in
-
💡 Pass by Value vs Pass by Reference in JavaScript (Simple Explanation) If you're learning JavaScript, understanding how data is passed is crucial 👇 🔹 Pass by Value (Primitives) When you assign or pass a primitive type (number, string, boolean, null, undefined, symbol, bigint), JavaScript creates a copy. let a = 10; let b = a; b = 20; console.log(a); // 10 console.log(b); // 20 👉 Changing b does NOT affect a because it's a copy. 🔹 Pass by Reference (Objects) When you work with objects, arrays, functions or date objects, JavaScript passes a reference (memory address). let obj1 = { name: "Ali" }; let obj2 = obj1; obj2.name = "Ahmed"; console.log(obj1.name); // Ahmed console.log(obj2.name); // Ahmed 👉 Changing obj2 ALSO affects obj1 because both point to the same object. 🔥 Key Takeaway Primitives → 📦 Copy (Independent) Objects → 🔗 Reference (Shared) 💭 Pro Tip To avoid accidental changes in objects, use: Spread operator {...obj} Object.assign() Understanding this concept can save you from hidden bugs in real-world applications 🚀 #JavaScript #WebDevelopment #Frontend #Programming #CodingTips
To view or add a comment, sign in
-
🚀 JavaScript TDZ & Hoisting — Explained Simply Ever wondered why let & const throw ReferenceError, while var gives undefined? 🤔 Here’s the core idea 👇 🔹 Hoisting Declarations are processed during the compilation phase, before code execution. 🔹 Temporal Dead Zone (TDZ) For let and const, the variable exists but is inaccessible from the start of the block until it’s initialized. console.log(name); // ❌ ReferenceError let name = "Naveen"; 🔹 var vs let/const var → auto-initialized with undefined let/const → TDZ applies 📌 Key takeaway TDZ ends immediately for var, but only after initialization for let & const. 👉 Full explanation with examples on Medium 💬 CTA: Have you faced a TDZ bug in real projects or interviews? 👇 Comment “TDZ” or share your experience — let’s learn together! #JavaScript #Frontend #WebDevelopment #JSConcepts #InterviewPrep #Learning
To view or add a comment, sign in
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development