Day 3 of #30DaysOfJavaScript on LeetCode Today's challenge: 2704 — To Be Or Not To Be The task was to create a custom expect function that helps test code logic — much like how assertion libraries work in testing frameworks. Here’s my solution 👇 var expect = function(val) { return { toBe: function(otherVal) { if (val === otherVal) return true; else throw new Error("Not Equal"); }, notToBe: function(otherVal) { if (val !== otherVal) return true; else throw new Error("Equal"); } }; }; This challenge was a nice introduction to function objects and error handling in JavaScript — building a mini testing utility was fun! 💡 If you’d like to give it a shot, check it out here: https://lnkd.in/g6WC5mu7 #JavaScript #LeetCode #CodingChallenge #LearningJourney #Closures #30DaysOfCode #ErrorHandling #Testing
Creating a custom expect function for JavaScript testing on LeetCode
More Relevant Posts
-
Today's challenge was about JavaScript's async–await and error handling. I created a fetchData(success) function that sometimes resolves and sometimes rejects. Then I used try–catch inside the async function to handle the entire flow. Things I practiced: ✅ Creating a Custom Promise ✅ Proper use of async–await ✅ How resolve & reject work ✅ Error handling with try–catch ✅ API simulation (using setTimeout) Consistent practice is the key. Small steps → strong foundations. ✨ #javascript #asyncawait #webdevelopment #learninginpublic #mernstack #dsa #100daysofcode
To view or add a comment, sign in
-
-
🚀 Day 10 of My 30 Days of JavaScript Challenge 🧩 Problem: Allow One Function Call (LeetCode #2666) Given a function fn, return a new function that ensures fn can only be called once. The first call returns the actual result, and every subsequent call returns undefined. 💻 Language: JavaScript 📖 Problem Link: https://lnkd.in/epnrfahZ 💡 Solution: https://lnkd.in/eGQKWkEk 🧠 Concepts Used Closures to store state (whether function is already called) Function Wrapping Higher-Order Functions 📚 Takeaway This problem is a great example of how closures preserve state between function calls — an essential concept for: Memoization API rate-limiting Event listener control #Day10 #JavaScript #30DaysOfCode #LeetCode #WebDevelopment #CodingChallenge #Closures #FrontendDevelopment #100DaysOfCode
To view or add a comment, sign in
-
Sometimes the biggest bugs come from the smallest keywords. var doesn’t respect block scope — it leaks out and causes unexpected behaviour. In this visual breakdown, you’ll see: ✅ Why var creates hidden “ghost variables” ✅ How let solved the problem with real block scope ✅ The simple rule: const > let >>> var Cleaner code starts with better defaults. 💡 Follow CodebreakDev — Let’s CodeBreak it down, together ⚡ Hashtags: #JavaScript #CleanCode #WebDevCommunity #DeveloperTips #ES6 #CodebreakDev
To view or add a comment, sign in
-
🚀 Day 19 of 30 Days of JavaScript – LeetCode Problem: 1207. Unique Number of Occurrences Today’s challenge was all about checking whether the number of occurrences of each value in an array is unique. ✅ My Approach 1️⃣ Count occurrences I used a for...of loop to count how many times each element appears in the array. 2️⃣ Store frequency results This gives me an object holding the occurrence count of every unique item. 3️⃣ Convert to a Set I extracted the values and converted them into a Set using new Set(), since a set automatically removes duplicates. 4️⃣ Compare values Arrays and sets can’t be directly compared, so I converted both to strings using JSON.stringify() to compare their datatype + values. 5️⃣ Return result If both match, I return true; otherwise, false. #JavaScript #LeetCode #30DaysOfCode #codingjourney #developerlife
To view or add a comment, sign in
-
-
⚡ Day 2 – Deep Dive into Functions Day 2 was all about Functions in JavaScript! I learned about function declarations, expressions, and arrow functions — each has its own behavior and scope. Also discovered hoisting works differently for function declarations vs expressions. Tried a few mini exercises to check how parameters and return values behave. 💡 Key takeaway: Functions are first-class citizens in JS — they can be passed, returned, and stored like variables! 💬 What’s your favorite use of arrow functions? #JavaScript #Functions #FrontendLearning #CodingChallenge #TechJourney
To view or add a comment, sign in
-
🚀 Before async/await, we lived in callback hell. Nested code. Confusing logic. Sleepless debugging. Then async/await arrived — not just syntax, but sanity. ✅ Easier to read ✅ Easier to debug ✅ Easier to teach #JavaScript #CodingTips #AsyncAwait #DevelopersLife
To view or add a comment, sign in
-
-
#Day68 of #100DaysOfCode Topic: JavaScript Promises Concepts Covered: Understanding Asynchronous and Synchronous behavior Learning about Promise states (Pending, Fulfilled, Rejected) Using then() and catch() syntax Implementing Promise Chaining for cleaner asynchronous code Learned how Promises make async operations more manageable and improve code readability. Practiced in Code Playground console.log("Become the programmer you wish to be!"); Day 68/100 — Exploring the power of Asynchronous JavaScript #CCBP #jpnce #NxtWave #JavaScript #Promises #AsyncJS #WebDevelopment #100DaysOfCode #Day68 #LearningNeverStops
To view or add a comment, sign in
-
-
Error chaining offers a transparent view of what transpired and where. Matt Smith illustrates how `error.cause` simplifies debugging and logging in JavaScript. Read more: https://lnkd.in/gwyic6Gf
To view or add a comment, sign in
-
-
💡 𝗦𝗺𝗮𝗹𝗹 𝗠𝗶𝘀𝘁𝗮𝗸𝗲, 𝗕𝗶𝗴 𝗟𝗲𝘀𝘀𝗼𝗻 𝗶𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁! Today, I hit an error that said: '𝘥𝘦𝘭𝘦𝘵𝘦' 𝘤𝘢𝘯𝘯𝘰𝘵 𝘣𝘦 𝘤𝘢𝘭𝘭𝘦𝘥 𝘰𝘯 𝘢𝘯 𝘪𝘥𝘦𝘯𝘵𝘪𝘧𝘪𝘦𝘳 𝘪𝘯 𝘴𝘵𝘳𝘪𝘤𝘵 𝘮𝘰𝘥𝘦. At first glance, it looked confusing — but it turned out to be a great reminder about how JavaScript actually works under the hood. In JavaScript, the 𝘥𝘦𝘭𝘦𝘵𝘦 operator is meant to remove object properties, not variables. ✅ delete obj.key — works perfectly ❌ delete variableName — throws an error (especially in strict mode) 𝗪𝗵𝘆? Because variables declared with let, const, or var are not object properties — they belong to the current scope. Trying to “delete” them breaks the language rules. 𝗟𝗲𝘀𝘀𝗼𝗻 𝗹𝗲𝗮𝗿𝗻𝗲𝗱: If you just want to clear a variable, use variable = null; and let JavaScript’s garbage collector do the rest. Sometimes the smallest syntax errors teach the biggest fundamentals! 🚀 #JavaScript #TypeScript #WebDevelopment #CodingTips #LearningEveryday
To view or add a comment, sign in
-
🤯 Why is my async code not waiting inside forEach? I ran into this classic JavaScript trap last week. I needed to process a list of items one by one, each with an async operation: items.forEach(async (item) => { await processItem(item); }); console.log("All done!"); But… the log appeared before the processing finished. Even worse — some async calls overlapped unpredictably. 🧠 What’s actually happening? forEach doesn’t await the async callbacks you pass to it. It just runs them and moves on, without waiting for any of them to finish. So, console.log("All done!") runs immediately, not after everything is processed. ✅ The Fix If you need sequential async execution: for (const item of items) { await processItem(item); } console.log("All done!"); Or, if you want parallel execution: await Promise.all(items.map(processItem)); console.log("All done!"); 💡 Takeaway > forEach + async/await ≠ sequential execution. Use for...of for sequence, or Promise.all for parallelism. 🗣️ Your Turn Have you ever hit this bug while handling async tasks? Do you usually go for Promise.all or handle things one by one? #JavaScript #AsyncAwait #WebDevelopment #CodingTips #ES6 #FrontendDevelopment #DeveloperCommunity #CleanCode #ProgrammingInsights
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