Day 3: Deepening JavaScript Skills with Counter Function Challenge! 🚀 Today, I tackled a classic problem — creating a flexible counter object that can increment, decrement, and reset — and it really sharpened my understanding of closures and object-oriented design in JavaScript. I implemented the createCounter function to accept an initial value and return an object with three methods: increment(): increases the value by 1 and returns it decrement(): decreases the value by 1 and returns it reset(): resets the value to the initial value and returns it This exercise reinforced how closures help maintain persistent state for each counter instance, making the code both clean and efficient. Here's a quick snippet: javascript const createCounter = (init) => { let n = init; return { increment() { return ++n; }, decrement() { return --n; }, reset() { n = init; return n; } }; }; Excited to keep pushing forward on this JavaScript journey! 💪 Let’s continue learning and sharing—feel free to share your progress or ask questions below. #JavaScript #30DaysOfJS #CodingJourney #Closure #WebDevelopment #LeetCode #ProblemSolving
"Mastering JavaScript with a Counter Function Challenge"
More Relevant Posts
-
🚀 Day 2 of My 30 Days of JavaScript Journey ✅ Challenge: Counter Function (LeetCode #2620) Create a function createCounter(n) that returns another function. Each time the returned function is called, it should return the current value of n and then increment it by 1 — producing outputs like (n, n+1, n+2, ...). 💻 Language Used: JavaScript ❓ Problem Link: https://lnkd.in/gczTUyQ4 💡 Solution: https://lnkd.in/g2VVnv8u 🧠 Concept Highlighted: This problem reinforces the concept of closures — how JavaScript functions can "remember" variables from their outer scope even after the outer function has finished executing. It’s a foundational concept for mastering state management and functional programming in JS. 📘 My Takeaway: A small problem, but a powerful reminder that closures are at the heart of JavaScript. Understanding how functions preserve state paves the way for writing more efficient and modular code. Step by step, the logic becomes clearer! 💪 #JavaScript #LeetCode #30DaysOfCode #CodingChallenge #WebDevelopment #FrontendDevelopment #LearningEveryday #Closures
To view or add a comment, sign in
-
*5 Things I Wish I Knew When Starting JavaScript 👨💻* Looking back, JavaScript was both exciting and confusing when I started. Here are 5 things I wish someone had told me earlier: *1. var is dangerous. Use "let" and "const" "var" is function-scoped and can lead to unexpected bugs. "let" and "const" are block-scoped and safer. *2. JavaScript is asynchronous Things like "setTimeout()"and "fetch()" don’t behave in a straight top-down flow. Understanding the *event loop* helps a lot. *3. Functions are first-class citizens* – You can pass functions as arguments, return them, and even store them in variables. It’s powerful once you get used to it. *4. The DOM is slow – avoid unnecessary access* – Repeated DOM queries and manipulations can hurt performance. Use "documentFragment" or batch changes when possible. *5. Don’t ignore array methods* map(), "filter()" , "reduce()", "find()"… they make code cleaner and more readable. I wish I started using them sooner. 💡 *Bonus Tip:* "console.log()" is your best friend during debugging! If you’re starting out with JS, save this. And if you're ahead in the journey — what do *you* wish you knew earlier? #JavaScript #WebDevelopment #CodingJourney #Frontend #LearnToCode #DeveloperTips
To view or add a comment, sign in
-
JavaScript for 15 Days – Day 5: Functions Today, You will learn about Functions, one of the most important concepts in JavaScript. A function is basically a reusable block of code that performs a specific task. It helps you write cleaner, more organized, and less repetitive code. Example: function greet(name) { return `Hello, ${name}!`; } console.log(greet("Moussa")); // Hello, Moussa! Why functions matter: - They make your code reusable and modular. - They improve readability. - They help you manage logic step by step. JavaScript also supports arrow functions and function expressions, which you’ll explore in slides! #JavaScript #FrontendDevelopment #CodingJourney #LearnToCode #WebDevelopment #15DaysJS #DevPerDay
To view or add a comment, sign in
-
🧠 Understanding Block Statements and Lexical Scope in JavaScript When I first started coding in JavaScript, I didn’t really pay attention to where I declared my variables — as long as the code ran, I was happy 😅. But once I began working on bigger projects, I realized scope and block behavior can make or break your code’s predictability. Here’s the thing: A block statement is simply the part inside { } — it could be inside an if, a for, or even just a standalone block. Example: { let message = "Hello world"; console.log(message); } console.log(message); // ❌ ReferenceError What’s happening? Because of lexical scoping, variables declared with let and const only exist within the block they were defined in. Meanwhile, var ignores that and leaks out — which is one reason modern JS avoids it. Understanding how lexical scope works helps prevent weird bugs and keeps your functions predictable. It’s one of those quiet fundamentals that makes your JavaScript more intentional and less chaotic. Have you ever been bitten by a var variable leaking out of a block before? 😅 #JavaScript #WebDevelopment #Frontend #CleanCode #JSFundamentals
To view or add a comment, sign in
-
-
🚀 Day 2 — Mastering Closures in JavaScript Today I explored one of the most powerful concepts in JavaScript — Closures. It’s amazing how a function can “remember” the variables from its outer scope even after that outer function has finished executing! 🧠 Here’s what I learned and understood today: 🔍 What is a Closure: A closure is formed when an inner function remembers and accesses variables from its outer function — even after the outer function has returned. 🧩 Lexical Scope: JavaScript uses lexical scoping — meaning, a function’s scope is determined by where it is defined, not where it’s called. ⚙️ Closure Use Cases: Used for data privacy, maintaining state, creating function factories, and managing async operations elegantly. 🔁 Closures in Loops: I learned how closures help capture the correct variable values inside loops — especially when using var, let, and asynchronous code. 🔒 Private Variables with Closures: Closures can hide data from the global scope, allowing us to create private variables and methods, just like in OOP. 📦 Module Pattern with Closures: The module pattern uses closures to bundle related functionality together, providing encapsulation and privacy — the backbone of reusable JS design. 🧮 Memory Implications: Since closures keep references to outer variables, understanding memory management is crucial to prevent leaks in long-running applications. I learn how JavaScript manages scope, memory, and data privacy behind the scenes. #JavaScript #Closures #WebDevelopment #Frontend #Day2 #Coding #LearningJourney
To view or add a comment, sign in
-
🚀 Day 4 of My 30 Days of JavaScript Journey ✅ Challenge: Create Counter Object (LeetCode #2665) Write a function createCounter(init) that returns an object with three functions: increment() → increases the value by 1 and returns it. decrement() → decreases the value by 1 and returns it. reset() → resets the value to the initial value and returns it. 💻 Language Used: JavaScript ❓ Problem Link: https://lnkd.in/gKxPxb8Y 💡 Solution: https://lnkd.in/g3jv6Nfi 🧠 Concept Highlighted: This challenge strengthens understanding of closures and object methods in JavaScript. It shows how functions can preserve and modify internal state — a key concept in building dynamic, stateful applications. #JavaScript #LeetCode #30DaysOfCode #CodingChallenge #WebDevelopment #FrontendDevelopment #LearningEveryday #Closures #ProblemSolving
To view or add a comment, sign in
-
I Thought I Knew JavaScript… Until I Compared Two Objects 😅 🚀 Today I learned something that seems simple at first — but it completely changed how I think about JavaScript objects! In JavaScript, objects, arrays, and functions are compared by reference, not by value. That means: const obj1 = { a: 1, b: 2 } const obj2 = { a: 1, b: 2 } console.log(obj1 === obj2); // false Even though they look identical, they’re actually stored in different memory locations. Each time you write { a:1, b:2 }, you’re creating a new object in memory. However, if you assign one object to another variable: const obj1 = { a:1, b:2 }; const obj2 = obj1; console.log(obj1 === obj2); // true Now both variables reference the same memory address — so a change in one reflects in the other. Understanding this subtle difference between reference and value comparison helped me better grasp how caching, memoization, and equality checks work under the hood in JS. Takeaway: Objects are shared by reference, but compared by memory address. #JavaScript #WebDevelopment #Learning #CodingJourney #TechInsights
To view or add a comment, sign in
-
💻 JavaScript Code Challenges: Level Up Your Skills 💻 Want to truly master JavaScript? Stop memorizing and start practicing with real engine-level challenges: 1️⃣ Scope & Lexical Environment let a = 10; function test() { let a = 20; console.log(a); } test(); // ? console.log(a); // ? 2️⃣ Closures function counter() { let count = 0; return function() { count++; return count; } } const inc = counter(); console.log(inc()); // ? console.log(inc()); // ? 3️⃣ Hoisting & TDZ console.log(x); // ? let x = 5; console.log(y); // ? var y = 10; ✅ Challenge yourself: Predict the output before running the code. Understand why it behaves that way. That’s how you think like the JS engine! #JavaScript #CodingChallenge #Closures #Scope #Hoisting #LexicalScoping #TDZ #DevCommunity #WebDevelopment #ProgrammingTips #CleanCode
To view or add a comment, sign in
-
*5 Things I Wish I Knew When Starting JavaScript 👨💻* Looking back, JavaScript was both exciting and confusing when I started. Here are 5 things I wish someone had told me earlier: *1. `var` is dangerous. Use `let` and `const`* – `var` is function-scoped and can lead to unexpected bugs. `let` and `const` are block-scoped and safer. *2. JavaScript is asynchronous* – Things like `setTimeout()` and `fetch()` don’t behave in a straight top-down flow. Understanding the *event loop* helps a lot. *3. Functions are first-class citizens* – You can pass functions as arguments, return them, and even store them in variables. It’s powerful once you get used to it. *4. The DOM is slow – avoid unnecessary access* – Repeated DOM queries and manipulations can hurt performance. Use `documentFragment` or batch changes when possible. *5. Don’t ignore array methods* – `map()`, `filter()`, `reduce()`, `find()`… they make code cleaner and more readable. I wish I started using them sooner. 💡 *Bonus Tip:* `console.log()` is your best friend during debugging! If you’re starting out with JS, save this. And if you're ahead in the journey — what do *you* wish you knew earlier? #JavaScript #WebDevelopment #CodingJourney #Frontend #LearnToCode #DeveloperTips
To view or add a comment, sign in
-
🚀 JavaScript Core Concept: Hoisting Explained Ever wondered why you can call a variable before it’s declared in JavaScript? 🤔 That’s because of Hoisting — one of JavaScript’s most important (and often misunderstood) concepts. When your code runs, JavaScript moves all variable and function declarations to the top of their scope before execution. 👉 But here’s the catch: Variables (declared with var) are hoisted but initialized as undefined. Functions are fully hoisted, meaning you can call them even before their declaration in the code. 💡 Example: console.log(name); // undefined var name = "Ryan"; During compilation, the declaration var name; is moved to the top, but the assignment (= "Ryan") happens later — that’s why the output is undefined. 🧠 Key Takeaway: Hoisting helps JavaScript know about variables and functions before execution, but understanding how it works is crucial to avoid tricky bugs. #JavaScript #WebDevelopment #Frontend #ProgrammingConcepts #Learning #Hoisting #CodeTips
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