🚀 Leveling up my JavaScript logic: The Fisher-Yates Shuffle! I spent some time today diving into how online examination systems handle fairness and randomization. To solve the problem of generating unique question orders for every student, I implemented the Fisher-Yates Shuffle Algorithm. 💡 Why this approach? While many might use array.sort(() => Math.random() - 0.5), that method is actually biased and doesn't provide a truly uniform distribution. The Fisher-Yates (or Knuth) Shuffle is the gold standard because: Efficiency: It runs in O(n) time complexity. True Randomness: Every permutation of the array is equally likely. In-place potential: It can be done without extra memory (though I used the spread operator here to keep the original data immutable! 🛡️). 🛠️ The Implementation I wrapped the logic in a Higher-Order Function. This allows me to lock in the "Original Array" and generate a fresh, shuffled version whenever getPaper() is called. Key takeaway for the day: Functional programming isn't just about cleaner code; it’s about creating predictable, reusable tools for complex problems. Check out the snippet in the image below! 👇 #JavaScript #WebDevelopment #CodingLife #Algorithms #ProblemSolving #FisherYates #LearningToCode #SoftwareEngineering How do you handle randomization in your projects?
Implementing Fisher-Yates Shuffle for Fair Randomization in JavaScript
More Relevant Posts
-
Day 10 of #100DaysOfCode: Optimizing Logic & Cleaning Syntax 🧹 Today was about efficiency. I didn't just write new code; I looked at how to make code run faster and look cleaner. DSA: The Square Root Trick I tackled Prime Number logic. The beginner way is to check every number from 2 to n-1. That’s O(n). But I learned you only actually need to check up to the square root of n. Result: The complexity drops to O(sqrt{n}). Lesson: Math saves computing power. Development: Modern JavaScript I revisited my BMI Calculator (from Day 2) and refactored the whole thing. I swapped out the old function declarations for Arrow Functions. Old: function calculate(weight, height) { ... } New: const calculate = (weight, height) => { ... } It’s a small syntax change, but it makes the code so much more readable and concise. Double digits! 10 days down. 🚀 #JavaScript #DataStructures #Optimization #WebDevelopment #CodingJourney
To view or add a comment, sign in
-
Headline: 🧠 Thinking Mathematically: Finding the GCD of Strings The Content: Continuing my JavaScript challenge on LeetCode! Today I tackled the "Greatest Common Divisor of Strings" problem. This one is fascinating because it blends string logic with classic number theory. The Challenge: Given two strings, find the largest string x such that x divides both str1 and str2. My Approach: The Concatenation Test: A brilliant trick for this problem is checking if str1 + str2 === str2 + str1. If they don't match, a common divisor string literally cannot exist. The GCD Logic: If they pass the test, the length of the greatest common divisor string must be the GCD of the lengths of the two strings. The Solution: I implemented a recursive helper function using the Euclidean Algorithm to find the gcdLength, then simply sliced the prefix! Technical Efficiency: Time Complexity: $O(n + m)$ due to string concatenation and comparison. Space Complexity: $O(n + m)$ to store the concatenated strings. Key Reflection: It’s amazing how concepts from basic math (like GCD) find their way into modern software engineering problems. It’s all about finding the pattern before writing the first line of code. Onward to the next challenge! 📈 #LeetCode #JavaScript #CodingChallenge #ProblemSolving #SoftwareEngineering #Algorithms #MathInCode #WebDevelopment
To view or add a comment, sign in
-
-
✨ 𝗗𝗮𝘆 𝟰 𝗼𝗳 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 🚀 Today I learned about 𝗖𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹𝘀, 𝗟𝗼𝗼𝗽𝘀, 𝗮𝗻𝗱 𝗢𝗽𝗲𝗿𝗮𝘁𝗼𝗿𝘀 — the core logic that makes programs think and repeat tasks. But what really surprised me was what happens behind the scenes when we compare values. I discovered why floating-point calculations can be unpredictable in JavaScript. 𝗙𝗼𝗿 𝗲𝘅𝗮𝗺𝗽𝗹𝗲: 𝟬.𝟭 + 𝟬.𝟮 !== 𝟬.𝟯 This happens because JavaScript uses binary floating-point representation (IEEE 754), and some decimal numbers can’t be represented exactly in binary. So tiny precision errors appear. It’s fascinating to see that even simple math has deep computer science concepts behind it. Every day I realize: writing code is one thing, understanding it deeply is another. 💪 #JavaScript #100DaysOfCode #WebDevelopment #LearningJourney #ComputerScience
To view or add a comment, sign in
-
-
✅ Solved LeetCode: Binary Tree Postorder Traversal (145) Implemented a recursive Postorder Traversal in JavaScript, following the sequence: Left → Right → Root. The solution uses a helper function traversal(curr) that: - First explores the left subtree, - Then explores the right subtree, - And finally processes the current node by pushing its value to the result array. This traversal is especially useful in scenarios like deleting a tree, evaluating expression trees, or bottom-up processing of nodes. ⏱ Time Complexity: O(n) — every node is visited once 🧠 Space Complexity: O(h) — recursion stack, where h is the height of the tree A key traversal technique every DSA learner should master! 🌳🚀
To view or add a comment, sign in
-
-
Day 20: Property Descriptors & The "Sugar" of Classes 🏗️💎 Object-oriented JavaScript isn't just about syntax; it's about control. Today was the final deep-dive into the "Meta" layer of JS objects—understanding why Math.PI is immutable and how to build bulletproof class structures. The "Crack-It Kit" Checklist: Day 20 📑 🔹 Class Hierarchy: Mastering extends and the super() bridge. Understanding why the parent must initialize before the child can exist. 🏛️ 🔹 Static Utilities: Learning to define methods that belong to the "Blueprint" (Class) rather than the "House" (Instance). ⚙️ 🔹 The .bind() Marriage: Locking context permanently. Moving beyond immediate execution to creating reusable, context-safe functions. 🔗 🔹 Under the Hood (Descriptors): Breaking down writable, enumerable, and configurable. Solving the mystery of why built-in JS properties are unchangeable. 💎 🔹 Property Shielding: Using Object.defineProperty to hide data from loops and prevent unauthorized overwrites. 🛡️ 🔹 Object Iteration: Mastering Object.entries() and Factory Functions for cleaner, more modern data handling. 🧪 The foundation is complete. We’ve moved from basic literals to professional meta-programming. 🏗️ #JavaScript #WebDevelopment #CrackItKit #OOP #WebEngineering #CodingJourney #SoftwareArchitecture #TechInterviews #120DayChallenge
To view or add a comment, sign in
-
-
Day 1 – DSA Journey | Arrays 🚀 Today, I solved two classic array problems on LeetCode and focused more on understanding the logic than just getting accepted solutions. ✅ Problems Solved 📌 Two Sum 📌 Median of Two Sorted Arrays 🔹 Two Sum Approach: I used a HashMap (Map in JavaScript) to store previously seen numbers and their indices. For each element, I calculated the subtarget = target - currentValue and checked if it already existed. Why this works: Instead of checking every pair (O(n²)), hashing lets us find the complement in constant time. What I Learned: ✅ Hashing is powerful for lookup-based problems ✅ Always think about reducing nested loops Complexity: ⏱ Time: O(n) 📦 Space: O(n) 🔹 Median of Two Sorted Arrays Approach: This problem was less about coding and more about clear thinking. I used binary search on the smaller array to find the correct partition such that: Left half contains smaller elements Right half contains larger elements Handled edge cases using -Infinity and Infinity to avoid extra conditions. What I Learned: ✅ Binary search is not just for searching elements ✅ Partition logic + edge cases decide correctness ✅ Understanding the math behind the problem is key Complexity: ⏱ Time: O(log(min(n, m))) 📦 Space: O(1) 🧠 Takeaway Some problems are solved by data structures. Some problems are solved by clear thinking. Both matter. On to Day 2 — staying consistent 🔁🚀 #DSA #Arrays #LeetCode #JavaScript #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
🚀 Day 12:– JavaScript + DSA (Java) 🔹 JavaScript: ✅ Learned reduce() function deeply (accumulator, current value, initial value). ✅ Explored Set and its important properties: set.add(value) set.delete(value) set.has(value) set.clear() set.size Stores only unique values ✅ Practiced Map and its methods: map.set(key, value) map.get(key) map.has(key) map.delete(key) map.clear() map.size Can store any type of key (objects, numbers, etc.) 🔹 DSA: Entered into the world of Recursion and solved basic problems like: Print 1 to N Print N to 1 Sum of N numbers Fibonacci Factorial And more…... #JavaScript #DSA #Recursion #WebDevelopment #CodingJourney
To view or add a comment, sign in
-
⚠️ That One-Liner Math.max(...arr) Might Be Slowing You Down We’ve all written this: Math.max(...arr) It’s clean. It’s readable. It feels very JavaScript-y. 😌 But here’s the uncomfortable truth: It’s not always safe in production. What Actually Happens Behind the Scenes? When you use: Math.max(...largeArray) JavaScript doesn’t “loop” the array. It expands every single element into a function argument. If your array has 10 elements → fine. If it has 10,000 → risky. If it has 100,000+ → 💥 you might hit: ❌ Maximum call stack size exceeded ❌ Engine argument limits ❌ Memory spikes ❌ Unexpected crashes Most JS engines have limits on how many arguments a function can accept. And yes — spread turns your array into individual arguments. 🧠 The Safer Alternative Instead of relying on argument expansion: arr.reduce((max, val) => val > max ? val : max, -Infinity) Why this is better: ✔️ No argument explosion ✔️ Handles very large arrays safely ✔️ More predictable performance ✔️ Production-friendly. 🚀 The Real Lesson Modern syntax ≠ Always better. As engineers, especially when building scalable systems, we should ask: What does this do under the hood? How does it behave with large inputs? Will this break at scale? Clean code isn’t just about shorter lines. It’s about understanding trade-offs. If you’ve ever debugged a production issue caused by a “beautiful one-liner,” you know the pain. 😅 What’s another JavaScript shortcut that looks elegant but hides a performance trap? 👇 #JavaScript #WebDevelopment #FrontendDevelopment #Programming #SoftwareEngineering #CleanCode #PerformanceOptimization #TechLeadership #CodingTips #Developers
To view or add a comment, sign in
-
-
Shortest Path Algorithm — Explained Mathematically We often learn shortest path algorithms from a coding perspective. But what if we step back and understand them from a mathematical point of view? In today’s post, I’ve explained the shortest path algorithm in a slightly different way — through mathematical reasoning and structure. It’s a deeper, more conceptual approach that helps you truly understand *why* the algorithm works, not just how to implement it. If you enjoy digging beneath the surface and building strong theoretical foundations behind practical coding problems, this one is for you. 👇 Do you prefer understanding algorithms conceptually first, or jumping straight into code? Follow Muhammad Nouman for more useful content #learningoftheday #1000daysofcodingchallenge #FrontendDevelopment #WebDevelopment #JavaScript #React #Next #CodingCommunity #Algorithms #DataStructures #SystemDesign #ProblemSolving
To view or add a comment, sign in
-
🚀 Cracked a tricky DSA concept today — Search in Rotated Sorted Array (with duplicates) At first glance the array looks unsorted… But actually it’s two sorted arrays joined together 🔍 Example: [2,5,6,0,0,1,2] The challenge ❓ Find a target efficiently (not O(n)) → Use Modified Binary Search (O(log n)) 💡 Key Learning: 1️⃣ One half of the array is always sorted 2️⃣ Check if target lies in the sorted half 3️⃣ If duplicates confuse the search → shrink the window (left++, right--) Core logic: • All equal → ignore edges • Left sorted → search there • Else → search right side 📈 What I learned: Understanding the logic is more important than memorizing code. Today I didn’t just solve a problem — I understood how to think like an algorithm. #DSA #BinarySearch #JavaScript #CodingJourney #100DaysOfCode #ProblemSolving
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