Day 4 of #30DaysOfJavaScript on LeetCode Today's challenge: 2665 — Counter II 🔁 The task was to create a createCounter function that accepts an initial integer init and returns an object with three functions: increment() → increases the value by 1 decrement() → decreases the value by 1 reset() → resets the value back to init Here’s my solution 👇 var createCounter = function(init) { let cache = init; return { increment: function() { return ++init; }, reset: function() { init = cache; return cache; }, decrement: function() { return --init; } }; }; This challenge was a good exercise in understanding closures and object methods — it’s cool how functions can maintain state in JavaScript! 💡 If you’d like to try it out, check it here: https://lnkd.in/g6WC5mu7 #JavaScript #LeetCode #CodingChallenge #LearningJourney #Closures #30DaysOfCode #Functions #Objects #Programming
Solved LeetCode challenge 2665 — Counter II with JavaScript
More Relevant Posts
-
JavaScript Closures Explained 💡 A closure is a powerful feature in JavaScript where an inner function retains access to variables from its outer function, even after the outer function has finished executing. This means the inner function "remembers" the environment it was created in. Closures enable data encapsulation, function factories, and help with keeping state in asynchronous code. Example: function outer() { let count = 0; return function inner() { count++; console.log(count); }; } const counter = outer(); counter(); // 1 counter(); // 2 counter(); // 3 Here, inner remembers the count variable even after outer is executed. This is what makes closures so useful! #JavaScript #Closure #WebDevelopment #JavaScriptClosures #Coding #Programming #LearnJavaScript #FrontendDevelopment #DevTips #JavaScriptTips #CodeNewbie
To view or add a comment, sign in
-
Day 8 of #30DaysOfJavaScript on LeetCode Today's challenge: 2629 — Function Composition The task was to implement a function that takes an array of functions [f1, f2, f3, ..., fn] and returns a new function that represents their composition. In simple terms, the composed function should apply all the given functions from right to left, just like: f(g(h(x))) Here’s my solution 👇 var compose = function(functions) { return function(x) { let res = x; for (let i = functions.length - 1; i >= 0; i--) { res = functions[i](res); } return res; } }; This challenge gave me a deeper understanding of how function chaining and composition work in JavaScript — building complex logic from smaller, reusable functions. It’s a beautiful example of how functional programming principles simplify problem-solving! Try it out here : https://lnkd.in/g6WC5mu7 #JavaScript #LeetCode #CodingChallenge #LearningJourney #30DaysOfCode #Functions #Callbacks #Programming #Composition
To view or add a comment, sign in
-
-
Day 6 of #30DaysOfJavaScript on LeetCode Today's challenge: 2634 — Filter Elements from Array The task was to implement a custom filter function that returns a new array containing only the elements that satisfy a given condition — similar to the built-in Array.filter() method, but without using it. Here’s my solution 👇 var filter = function(arr, fn) { let ans = []; for (let i = 0; i < arr.length; i++) { if (fn(arr[i], i)) ans.push(arr[i]); } return ans; }; This challenge was a great way to understand how callback functions and conditional logic work together in JavaScript — it really helps appreciate how built-in methods like filter() operate under the hood! If you’d like to try it out, check it here: https://lnkd.in/g6WC5mu7 #JavaScript #LeetCode #CodingChallenge #LearningJourney #30DaysOfCode #Functions #Callbacks #Arrays #Programming #Filter
To view or add a comment, sign in
-
-
💡 JavaScript’s const isn’t always constant. When I first learned JavaScript, I thought const meant “this value can never change.” But then I did this 👇 𝚌𝚘𝚗𝚜𝚝 𝚞𝚜𝚎𝚛 = { 𝚗𝚊𝚖𝚎: "𝙰𝚕𝚎𝚡", 𝚊𝚐𝚎: 𝟸𝟻 }; 𝚞𝚜𝚎𝚛.𝚊𝚐𝚎 = 𝟸𝟼; 𝚌𝚘𝚗𝚜𝚘𝚕𝚎.𝚕𝚘𝚐(𝚞𝚜𝚎𝚛); // { 𝚗𝚊𝚖𝚎: "𝙰𝚕𝚎𝚡", 𝚊𝚐𝚎: 𝟸𝟼 } …and it worked. 🤯 That’s when I learned: 👉 const prevents reassignment, not mutation. You can’t reassign the variable itself: 𝚞𝚜𝚎𝚛 = { 𝚗𝚊𝚖𝚎: "𝚂𝚊𝚖" }; // ❌ 𝙴𝚛𝚛𝚘𝚛 But you can modify its internal properties — because the reference in memory stays the same. 🧠 In short: const stops you from changing what the variable points to but not what’s inside the object or array So next time you use const, remember — it’s constant in reference, not in content. #javascript #webdevelopment #backend #programming #learning #constants #mutability
To view or add a comment, sign in
-
Count the Number of Digits - JavaScript Logic Simplified 👉 Day 24 / Day 93👈 👉 Find how many digits a number has — without converting it to a string? 1️⃣ Handle 0 as a special case — it has 1 digit. 2️⃣ Use Math.abs() to support negative numbers. 3️⃣ Repeatedly divide the number by 10 until it becomes 0, increasing the counter each time. 💡 Example: 👉 23453 → 2345 → 234 → 23 → 2 → 0 ✅ Total digits = 5 #JavaScript #CodingTips #WebDevelopment #FrontendDeveloper #LearnToCode #ProgrammingLogic #CleanCode #ProblemSolving #100DaysOfCode #TechCommunity #CodeNewbie #Algorithms
To view or add a comment, sign in
-
-
Tired of JavaScript fundamentals feeling fuzzy? 🤯 If concepts like Hoisting, Closures, and Prototypes slow you down, you need a clearer reference. I've distilled the core structure rules of the language into Cheat Sheet Part 1: JavaScript Static Core for developers. Quickly Master: - Scope & TDZ: Variable boundaries (let/const vs var). - Hoisting Logic: What the engine really moves. - Closures: How functions create private memory. - Prototypes: The true foundation of JS inheritance. Stop guessing, start coding with confidence. ➡️ View the attached PDF now to get your free copy! 💾 #JavaScript #WebDevelopment #CodingTips #Programming #CheatSheet
To view or add a comment, sign in
-
🚀 Learning Matrix Manipulation in JavaScript Today I learned how to handle matrices in JavaScript! I implemented a function called zeroMatrix() that sets entire rows and columns to 0 if any element in the matrix is 0. This helped me understand how nested loops, logical conditions, and 2D arrays work together — a great hands-on exercise for improving problem-solving skills in JS. #JavaScript #CodingJourney #WebDevelopment #LearningInPublic
To view or add a comment, sign in
-
-
Let’s decode three of the most confusing JavaScript topics 👇 🔹 1. Scope Scope defines where your variables and functions are accessible. Global Scope: Accessible everywhere in the code. Function Scope: Accessible only inside a function. Block Scope: (introduced with let & const) accessible only within { }. 🔹 2. Hoisting JavaScript moves declarations to the top of their scope before code execution. But remember — only declarations are hoisted, not initializations. That’s why var behaves differently than let and const. 🔹 3. TDZ (Temporal Dead Zone) The period between entering a scope and initializing a variable declared with let or const. Accessing a variable in TDZ results in a ReferenceError — it exists but isn’t accessible yet! #JavaScript #FrontendDevelopment #WebDevelopment #Coding #Programming #LearnToCode #Scope #Hoisting #TDZ
To view or add a comment, sign in
-
-
javascript Logic Challenge #3 how to count the digits of a number in JS Simple Explanation : Imagine a number like 567. We want to know how many numbers are inside it. So we do this: 1. Take the number 2. Remove the last digit 3. Each time you remove a digit - count +1 4. Stop when the number becomes 0 That’s it! Example: 567 - 3 digits I used a simple loop that removes one digit at a time and counts it. #JavaScript #Learning #LogicBuilding
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
“You just earned a new follower! Let’s keep inspiring each other 💫🔥” Anubama I