Day 27 of #30DaysOfJavaScript on LeetCode Today’s Challenge: 2705 – Compact Object Today’s problem focused on cleaning JSON data by recursively removing all false values from objects and arrays. Here's my solution: var compactObject = function(obj) { if (Array.isArray(obj)) return obj.filter(Boolean).map(compactObject); if (typeof obj !== 'object' || obj === null) return obj; const ans = {}; for (const key in obj) { const value = compactObject(obj[key]); if (Boolean(value)) { ans[key] = value; } } return ans; }; This approach uses recursion to traverse deeply nested structures while ensuring that only truthy values remain in the final output. 🔗 Try the problem here: https://lnkd.in/g6WC5mu7 #JavaScript #LeetCode #CodingChallenge #LearningJourney #WebDevelopment #Developers #FrontEndDevelopment #30DaysOfCode #30DaysOfJavaScript
Anubama I’s Post
More Relevant Posts
-
Mastering JavaScript: The Power of .reduce(): The Array.reduce() method is a powerhouse in JavaScript, often hailed as the "Swiss Army Knife" for array transformations. While it might seem a bit intimidating at first glance, understanding its core principle can unlock a new level of efficiency and elegance in your code. Think of reduce as a data synthesizer: it takes an array of individual items and, through a "reducer" function, combines them step-by-step into a single, accumulated result. This could be a sum, an object, or even a flattened array! I recently tackled LeetCode Problem 2626: "Array Reduce Transformation," which challenged us to implement our own version of this fundamental method. Here's a clean, efficient solution: #JavaScript #WebDevelopment #CodingTips #FunctionalProgramming #JavaScript #LeetCode #WebDevelopment #CodingChallenge #SoftwareEngineering #ProgrammingTips #FrontendDevelopment #DataTransformation #TechSolutions
To view or add a comment, sign in
-
-
What does this print? (Hint: Most developers get it wrong.) I thought I UNDERSTOOD JAVASCRIPT loops... until I saw this bug. 😅 Quick quiz: What does this code print? for (var i = 0; i < 3; i++) { setTimeout(() =>console.log(i), 1000); } If you said 0, 1, 2, you’re falling into the same trap I did early. THE ACTUAL OUTPUT IS 3, 3, 3. Why? It comes down to Scope and the Event Loop: var is function-scoped (not block-scoped). The setTimeout callback doesn't run until the loop has already finished. By the time the console logs, i has already been incremented to 3. The Fix? Simply switching to let (block-scoping) or using a closure. It’s a classic example of why understanding the engine under the hood is more important than just knowing the syntax. Have you ever been burned by var in a modern codebase? #JavaScript #WebDev #CodingTips #SoftwareEngineering
To view or add a comment, sign in
-
-
1. Parsing: First, the engine breaks down the code into tokens. Tokens are small units like keywords, operators, literals, and identifiers. Then, it parses the tokens to create an Abstract Syntax Tree (AST), which represents the grammatical structure of the code. Compilation: Modern JavaScript engines like V8 (used in Chrome and Node.js) use a technique called Just-In-Time (JIT) compilation. This involves compiling JavaScript code into machine code at runtime. 2. Execution: This is when we see the effects of our code in the browser. Initially, the engine interprets the JavaScript code directly from the AST, converting it into bytecode, which is then executed by the JavaScript Virtual Machine (JVM). The JIT compiler profiles the running code to identify “hot” code paths that are executed frequently. These paths are recompiled into highly optimized machine code for faster execution. 3. Garbage Collection: The engine performs memory management through garbage collection. It periodically scans for and cleans up unused or unreachable memory to free up resources. 4. Event Loop: Since JavaScript is single-threaded, it uses the event loop to manage the execution of asynchronous code. It continuously checks the call stack, and the message queue processes events and executes callback functions when the call stack is empty. #JavaScript, #WebDevelopment, #SoftwareDeveloper, #FrontendDeveloper, #FullStackDeveloper ,#Programming, #Coding ,#Tech, #DeveloperCommunity
To view or add a comment, sign in
-
-
🔥 Day 26 of #30DaysOfJavaScript #LeetCode 2705: Compact Object Today I worked on a really interesting problem — building a recursive function that removes all falsy values from an object or array, including deeply nested ones. Falsy values include: false, 0, "", null, undefined, NaN The twist? 👉 Arrays are also treated as objects 👉 And we must compact every nested level 👉 Without using any built-in magic ✨ 🧠 Key Learnings ✅ Recursion is perfect for tree-like structures ✅ Array.isArray() helps treat arrays differently ✅ Boolean(value) is a clean way to test truthiness ✅ JSON constraints simplify the problem #JavaScript #LeetCode #30DaysOfCode #WebDevelopment #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
I had a function using Array.map(). The output was always undefined. At first I assumed: “map automatically returns transformed values.” ❌ Wrong assumption. Root cause: I forgot to return a value inside the callback. map does NOT mutate or guess logic. No return = undefined. Fix: Explicitly return the value. Small mistake, big logic lesson: JavaScript array methods are declarative, not magical. // ❌ Bug const nums = [1, 2, 3]; const result = nums.map(n => { n * 2; }); // ✅ Fix const fixed = nums.map(n => { return n * 2; }); #JavaScript #FrontendDevelopment #ProgrammingBasics #CleanCode #WebDevelopment
To view or add a comment, sign in
-
𝐒𝐭𝐨𝐩 𝐫𝐮𝐧𝐧𝐢𝐧𝐠 𝐲𝐨𝐮𝐫 𝐜𝐨𝐝𝐞 10 𝐭𝐢𝐦𝐞𝐬 𝐣𝐮𝐬𝐭 𝐭𝐨 𝐬𝐞𝐞 𝐨𝐧𝐞 𝐯𝐚𝐫𝐢𝐚𝐛𝐥𝐞. 🤦♂️ I recently discovered an extension named 𝐐𝐮𝐨𝐤𝐤𝐚.𝐣𝐬 and honestly, it has changed how I debug JavaScript/Typescript. Instead of: ❌ Writing code ❌ Adding console.log() ❌ Running the file ❌ Checking terminal ❌ Repeat 10 times... You get: ✅ Real-time inline results ✅ Live values as you type ✅ Instant feedback on every line 𝐍𝐨 𝐜𝐨𝐧𝐭𝐞𝐱𝐭 𝐬𝐰𝐢𝐭𝐜𝐡𝐢𝐧𝐠. Just pure coding flow. In the video: filtering and transforming user data with zero executions. Every variable value appears right there in the editor. This is one of those tools that makes you wonder how you coded without it. Check it out: https://quokkajs.com/docs/ Have you tried Quokka.js? What's your go-to VS Code extension? #JavaScript #Typescript #WebDevelopment #VSCode #DeveloperTools #Coding #SelfLearning
To view or add a comment, sign in
-
I was debugging a feature that kept producing wrong results, even though the logic looked correct. No errors. No crashes. Everything *seemed* fine. I kept changing the implementation, but nothing improved. The real issue wasn’t the code. It was an assumption I never questioned. I assumed my string was being modified in place. But in JavaScript, strings are immutable. So every change required creating a new copy. One missed reassignment was breaking the logic. Once that assumption was fixed, the solution was obvious. Bugs are symptoms, not the root cause 💡 #SoftwareEngineering #JavaScript #Debugging #WebDevelopment #DeveloperLife #LearningInPublic
To view or add a comment, sign in
-
-
JavaScript loops help us execute a block of code repeatedly until a condition is met. They are essential for handling arrays, objects, and repetitive tasks efficiently. 🔹 for loop – best for known iterations 🔹 while loop – runs while the condition is true 🔹 do...while loop – runs at least once 🔹 Clean syntax improves performance and readability 💡 Loops = less code, more logic, better control #JavaScript #JSLoops #ProgrammingBasics #WebDevelopment #CodingLife #LearnJavaScript
To view or add a comment, sign in
-
-
When something breaks in JS, knowing the type of error makes debugging way easier. ❌ SyntaxError Code is written incorrectly — JS can’t even start running it ❌ ReferenceError Trying to use something that doesn’t exist (variable or function not defined) ❌ TypeError Using a value in the wrong way (calling something that’s not a function, accessing undefined) Why it matters: ✅ Debug faster ✅ Fix the root cause ✅ Avoid random console guessing Errors aren’t the problem, understanding them is. Which one do you encounter most often? #JavaScript #JSBasics #FrontendDeveloper #WebDevelopment #Debugging #LearningInPublic
To view or add a comment, sign in
-
DAY 3 of me reading random docs and here what I got to learn today ........ BigInt in JavaScript (a living legend)🫡 Standard Javascript numbers are double-precision floats, meaning they hit a limit at Number.MAX_SAFE_INTEGER (2^53 -1). BigInts completely bypass this ceiling, allowing us to represent integers as large as system memory allows. Means literally they have not defined any limit....... but still there is engine limit means how big any object can be is defined by js engine like v8 but that too is quite large ..... JavaScript strictly prevents mixing BigInt and standard Number types in arithmetic. 10n + 5 Throws a TypeError 10n + 5n Works perfectly This isn't a bug it's a feature to prevent accidental precision loss when truncating a massive integer into a float. #JavaScript #WebDevelopment #Coding #LearningEveryday #BigInt #FrontendDev
To view or add a comment, sign in
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