🚀 Day 4 of my JavaScript Coding Practice Today’s problem: Two Sum var twoSum = function(nums, target) { const map = new Map(); // Store: { value : index } for (let i = 0; i < nums.length; i++) { const complement = target - nums[i]; // If the needed number is already in our map, we found the pair! if (map.has(complement)) { return [map.get(complement), i]; } // Otherwise, save the current number and its index map.set(nums[i], i); } return []; // Return empty if no pair is found }; 💡 Instead of using brute force (O(n²)), I used a HashMap approach to solve it in O(n) time. Key takeaway: Understanding how to trade space for time can significantly optimize performance. Small steps every day → Big improvements over time 📈 #JavaScript #DSA #CodingPractice #100DaysOfCode #FrontendDevelopment
Two Sum JavaScript Solution in O(n) Time
More Relevant Posts
-
One small word in JavaScript causes a lot of confusion: undefined. Many developers encounter it but don’t fully understand why it appears. In my latest article, I break it down in a practical way: • What undefined actually means • Common scenarios where it shows up • Mistakes developers often make • How to avoid it in real projects This is especially useful if you're learning JavaScript or improving your debugging skills. 🔗 Read the full article: https://lnkd.in/gaGDuAHp Curious — what was the most confusing JavaScript concept for you when you started? #JavaScript #WebDevelopment #FrontendDevelopment #Programming #Coding #SoftwareDevelopment #Debugging #LearnJavaScript
To view or add a comment, sign in
-
🔥 I used to think I knew JavaScript… until I discovered this. Early in my career, I was constantly fighting weird bugs: Variables leaking everywhere Random overrides Code breaking for “no reason” Then I stumbled upon this pattern: (function () { console.log('This runs immediately'); })(); It’s called an IIFE (Immediately Invoked Function Expression). This one concept completely changed how I understood scope in JavaScript. Before let and const existed, IIFEs were the go-to solution because they allowed us to: ✅ Execute code immediately ✅ Create private scopes ✅ Prevent polluting the global namespace ✅ Avoid naming collisions It felt like having a private sandbox in your code. But here’s the interesting part 👇 Today, we rarely need IIFEs anymore. Modern JavaScript gives us much cleaner tools: Block scoping with let and const True modules with import / export Arrow functions + better patterns So why should you still care about IIFEs in 2026? Because understanding why they existed helps you truly understand how JavaScript evolved — and why it behaves the way it does. The best developers don’t just learn syntax. They learn the history and the problems each feature was solving. 🧠 My takeaway: Don’t just learn what to use today. Learn why it existed in the first place. That’s what separates good developers from great ones. Have you ever used IIFEs in old codebases? Or did you start learning JavaScript after modules became the standard? Drop a comment 👇 I’d love to hear your experience. #JavaScript #WebDevelopment #Frontend #Programming #CleanCode #Coding
To view or add a comment, sign in
-
-
Day 6 / 30 - JavaScript Coding Practice Today’s problem: Roman to Integer var romanToInt = function (s) { if (s.length > 15) { return; } const romanMap = new Map([ ['I', 1], ['V', 5], ['X', 10], ['L', 50], ['C', 100], ['D', 500], ['M', 1000] ]); let sum = 0; let preValue = 0; for (let k = s.length - 1; k >= 0; k--) { let currentVal = romanMap.get(s[k]); if (currentVal < preValue) { sum -= currentVal; } else { sum += currentVal; } preValue = currentVal; } return sum; }; 💡 Key Insight: Instead of scanning from left to right, I iterated from right to left to easily handle subtraction cases like IV (4) and IX (9). 👉 If a smaller value comes before a larger one → subtract 👉 Otherwise → add #JavaScript #DSA #CodingPractice #ProblemSolving #100DaysOfCode #FrontendDevelopment
To view or add a comment, sign in
-
-
Assalam o Alaikum everyone, JavaScript Lesson 29 is here: Reference vs Value, Shallow Copy, Deep Copy & Immutable Patterns. This lesson covers one of the most important JavaScript concepts for writing clean and predictable code: understanding how values are stored, copied, and updated in memory. I explained the difference between primitive values and reference values, then demonstrated what happens when you assign objects and arrays directly. From there, I showed how to create shallow copies using spread syntax and Object.assign(), and why shallow copy can still cause problems with nested objects. I also covered deep copy using both: - JSON.parse(JSON.stringify()) - structuredClone() After that, I moved into immutable programming patterns, where I showed how to update arrays and objects without mutating the original data using: - filter() - map() - spread syntax - destructuring This is a very practical topic for JavaScript developers because immutability helps prevent bugs and makes code easier to maintain in real projects. Watch the lesson: https://lnkd.in/dSeTxYPP #JavaScript #ReferenceVsValue #ShallowCopy #DeepCopy #ImmutablePatterns #JavaScriptTutorial #WebDevelopment #FrontendDevelopment #Programming #CodingTutorial #DeveloperMaroof #LearnJavaScript #JavaScriptBasics #ModernJavaScript #SpreadOperator #ObjectAssign #StructuredClone #Immutability #JSConcepts #JavaScriptLessons
To view or add a comment, sign in
-
-
🚀 Day 7 / 30 - JavaScript Coding Practice Today’s challenge: Recreating the Array.map() functionality — without actually using it 👀 Problem: Apply a transformation function to each element of an array and return a new array. 💡 Key Insight: This problem helped me understand what’s happening under the hood of built-in methods like map(). 👉 Instead of relying on .map(), I used a loop to: Iterate through each element Apply the given function with both value & index Build a new transformed array Solution: var map = function (arr, fn) { let transArr = [] arr.forEach((element, i) => { transArr.push(fn(element, i) ?? element); }); return transArr; }; #JavaScript #DSA #CodingPractice #100DaysOfCode #FrontendDevelopment #ProblemSolving
To view or add a comment, sign in
-
-
🚀 JavaScript Essentials — closures, Math operators & recursion, but make it real Step by step, I’m building stronger JavaScript fundamentals through practice. In this homework, I worked on topics that are simple in theory, but much more interesting when you actually implement them yourself: ● Closures & state management ● Recursive functions ● Math methods and function binding with apply() / bind() 🛠 What I built in practice: ● counter() — a closure-based counter that remembers its state and can restart from any given number ● counterFactory() — a small counter object with .value(), .increment(), and .decrement() built with closures ● myPow(a, b, myPrint) — a recursive power function with a callback for formatted output ● myMax(arr) — finding the maximum value in an array using Math.max.apply() ● myMul(a, b) + myDouble() / myTriple() — reusing logic with bind() This task helped me better understand how JavaScript works with scope, closures, recursion, and reusable functional patterns. What I like about this kind of practice is that it turns abstract concepts into something tangible. Not just “I read it” — but “I built it, tested it, and now I actually get it.” 🔗 GitHub: https://lnkd.in/dHTBr-h3 Always learning. Always building. One function at a time 💻 "Coding like Zagreus: dying, retrying, and somehow making progress. ⚔️💻" #JavaScript #LearningByDoing #Closures #Recursion #MathOperators #FunctionalProgramming #Frontend #CodingJourney #WebDevelopment
To view or add a comment, sign in
-
-
🚀 Just Published: Map and Set in JavaScript https://lnkd.in/g-nAm7SZ Understanding Map and Set helps you write more efficient and cleaner JavaScript code. In this article, I covered: ✅ What Map is and how it stores key-value pairs ✅ What Set is and how it ensures unique values ✅ Difference between Map and Object ✅ Difference between Set and Array ✅ When to use Map and Set in real-world scenarios 💡 Learn how Map solves limitations of traditional objects and how Set automatically removes duplicates. If you're preparing for interviews or improving your JS fundamentals, this is a must-read! 🙏 Thanks to amazing mentors and community 🙌 Hitesh Choudhary Sir, Piyush Garg Sir, Akash Kadlag Sir, Suraj Kumar Jha Sir, Chai Aur Code #JavaScript #WebDevelopment #Coding #Frontend #FullStack #Programming #Developers #TechCommunity
To view or add a comment, sign in
-
-
🚀 Day 8 of My JavaScript Learning Journey Today I learned about Strings in JavaScript and explored various built-in methods to manipulate text. 📌 What I learned: • A String is a sequence of characters used to store text data • Strings are immutable (cannot be changed directly) • It is a primitive data type • String operations always return a new string ⚙️ String methods I practiced: ✔ length ✔ toUpperCase() / toLowerCase() ✔ trim() ✔ slice() / substring() ✔ replace() ✔ includes() ✔ indexOf() ✔ split() ✔ concat() 💡 I also practiced template literals: Hello ${name} → makes string formatting easier and cleaner. Understanding strings is very important because text handling is used in almost every application. Step by step, I’m improving my JavaScript fundamentals and coding skills. 💻✨ #JavaScript #WebDevelopment #FrontendDevelopment #CodingJourney #LearningInPublic #DeveloperJourney #ProgrammingBasics
To view or add a comment, sign in
-
-
🚀 Garbage Collection in JavaScript Ever wondered what happens to unused memory? 🤔 Let’s make it simple 👇 🧠 What is Garbage Collection? 👉 It is the process of automatically removing unused memory 👉 JavaScript handles this for you (no manual memory management) ⚡ How does JS decide what to remove? 👉 Using Reachability ✔ Reachable = Still in use ❌ Unreachable = Garbage 💡 Simple Example: 👉 If no variable is referencing an object 👉 It becomes eligible for garbage collection 🔥 Algorithm Used: 👉 Mark & Sweep ✔ Mark → Identify reachable objects ✔ Sweep → Remove unreachable objects ⚡ Key Understanding: 👉 If no reference points to an object 👉 It gets removed from memory 🚨 Important Note: 👉 Garbage collection is automatic 👉 You cannot control when it runs 💬 Did this concept clear your doubt? 📌 Save this for interviews (very important topic) #javascript #webdevelopment #frontend #coding #programming #javascriptdeveloper #learncoding #developers #100DaysOfCode
To view or add a comment, sign in
-
-
Understanding the Event Loop in JavaScript is a turning point for every developer. Many developers use async features like promises, setTimeout, or async/await daily — but very few truly understand what happens behind the scenes. I’ve written a detailed yet easy-to-understand article that breaks down: ✔ Call Stack ✔ Callback Queue ✔ Microtask Queue ✔ Execution Order If you want to strengthen your JavaScript fundamentals and avoid common async mistakes, this will definitely help. 👉 Read the full article: https://lnkd.in/gDhwvmUc I’d love to hear your thoughts — what was the hardest concept for you when learning the Event Loop? #JavaScript #SoftwareDevelopment #WebDevelopment #FrontendDevelopment #AsyncProgramming #Coding #TechLearning
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