🚀 Just solved the "Best Time to Buy and Sell Stock" problem in JavaScript! Today I worked on improving my problem-solving skills by implementing a solution to calculate the maximum profit from stock prices. 🔍 Problem: Given an array of prices, determine the maximum profit you can achieve by buying and selling once. 💡 Approach: I started with a brute-force solution using nested loops to compare every possible buy/sell pair. While not the most optimal (O(n²)), it helped me deeply understand the problem before optimizing. 📌 Example: Input: [10, 1, 5, 6, 7, 1] Output: 6 🧠 Key takeaway: Sometimes starting simple is the best way to build strong intuition before moving to more efficient solutions. 👉 Check out my code and more JavaScript patterns here: https://lnkd.in/ej4fNeZs #JavaScript #Coding #ProblemSolving #100DaysOfCode #WebDevelopment #LearningInPublic
Maximizing Stock Profit with JavaScript Problem-Solving
More Relevant Posts
-
Most people think they understand JavaScript classes. They don’t. Because JavaScript doesn’t even have real classes. What it actually has is prototypes — and classes are just syntactic sugar on top of them. Today in my T9 class, this completely changed how I look at JS: → Prototype-based inheritance (the actual mechanism) → Traditional vs Modern prototype handling → Constructor Functions (how things worked before classes) Then we moved to what most people are comfortable with: → Classes in JavaScript → constructor, extends, super, static → The 4 pillars of OOP: Encapsulation Inheritance Polymorphism Abstraction But here’s the uncomfortable truth: If you don’t understand prototypes, you don’t actually understand inheritance in JavaScript. You’re just memorizing syntax. That’s why debugging feels random. That’s why “this” behaves weird. That’s why things break in unexpected ways. Now it makes sense. Still early in the journey — but this was one of those “everything clicks” moments. Do you think JS should have stayed purely prototype-based, or are classes a good abstraction? #JavaScript #OOPS #WebDevelopment #LearningInPublic Thankyou Chai Aur Code Suraj Kumar Jha Sir Hitesh Choudhary Sir Piyush Garg Sir
To view or add a comment, sign in
-
-
Day 4 — Making Tech Simple. JavaScript looks simple… But here’s something most beginners don’t understand How does JavaScript handle multiple tasks at once if it’s single-threaded? The answer = Event Loop Here’s what actually happens: • Call Stack → Executes code one by one • Web APIs → Handle async tasks (setTimeout, fetch, events) • Callback Queue → Stores completed tasks • Event Loop → Pushes tasks back to stack when it’s free That’s how JavaScript handles async behavior without breaking. If you don’t understand this… 👉 Async code will always confuse you 👉 Debugging will feel hard But once you get it… Everything starts making sense 💡 📌 Day 4 of breaking down complex tech into simple visuals. Follow me if you want to actually understand JavaScript deeply. Comment “DAY 5” if you’re ready — Syed Shaaz Akhtar #JavaScript #WebDevelopment #Frontend #Programming #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Arrays are NOT real arrays in JavaScript. That was one of the most interesting things I learned today while studying Arrays in JavaScript. At first, arrays seem simple — just a collection of elements. But under the hood, JavaScript arrays are actually objects with special behavior, not fixed-size contiguous memory structures like in languages such as C++. What I learned about Arrays: • Arrays in JavaScript are mutable • They can store multiple data types • They are dynamic in size Key operations I practiced: • Adding/removing elements → push(), pop(), shift(), unshift() • Looping → classic for loop and modern iteration methods • Searching → indexOf(), lastIndexOf(), includes() • Manipulation → slice(), splice() • Conversion → join() (array → string) • Spread operator → modern and powerful way to copy/merge arrays One important insight: The sort() method can behave unexpectedly with numbers because it sorts values as strings by default. Example: [10, 2, 5].sort() // Output -> [10, 2, 5] To sort numbers correctly, we need a comparison function. To summarize everything, I created a detailed carousel Notes (Notes given by Rohit Negi). Course Instructor: Rohit Negi | Youtube Channel: Coder Army #JavaScript #WebDevelopment #LearningJourney #BuildInPublic #FrontendDevelopment #Fullstackdevelopment #Coding
To view or add a comment, sign in
-
Day 1 of 30 days of javascript challenge. problem-2667 Problem - Write a function createHelloWorld that returns another function, that returns "Hello World" As this is my first code, I revised my notes on javascript scope. ☑️ Function scope - Any variables declared inside a function body cannot be accessed outside the function body, but global variables can be used inside function body ☑️ Block scope - Any variable declared inside { } cannot be used outside the { } block, although it supports only let and const keyword, var can be used ☑️ Lexical scope - A variable declared outside a function can be accessed inside another function defined after the variable declaration. (The opposite is not true ) This problem uses the concept of closures and higher order functions. Please feel free to discuss where can I improve the code or if you have a different perspective, comment below your views. #javascript #coding #development #motivation #goals #leetcode #webdevelopment
To view or add a comment, sign in
-
-
𝐓𝐡𝐞 𝐜𝐚𝐥𝐥 𝐬𝐭𝐚𝐜𝐤 𝐡𝐚𝐬 𝐧𝐨 𝐩𝐚𝐭𝐢𝐞𝐧𝐜𝐞. 𝐈𝐭 𝐞𝐱𝐞𝐜𝐮𝐭𝐞𝐬 𝐞𝐯𝐞𝐫𝐲𝐭𝐡𝐢𝐧𝐠 — 𝐢𝐧𝐬𝐭𝐚𝐧𝐭𝐥𝐲, 𝐫𝐮𝐭𝐡𝐥𝐞𝐬𝐬𝐥𝐲, 𝐢𝐧 𝐨𝐫𝐝𝐞𝐫. So what happens when you need a 𝐝𝐞𝐥𝐚𝐲? That's where I hit a wall. If JavaScript is single-threaded and the call stack never pauses — how does '𝐬𝐞𝐭𝐓𝐢𝐦𝐞𝐨𝐮𝐭' even work? Turns out, it doesn't live in JavaScript at all. 𝐖𝐞𝐛 𝐀𝐏𝐈𝐬 — 𝐬𝐞𝐭𝐓𝐢𝐦𝐞𝐨𝐮𝐭, 𝐟𝐞𝐭𝐜𝐡( ), 𝐃𝐎𝐌 𝐞𝐯𝐞𝐧𝐭𝐬, 𝐥𝐨𝐜𝐚𝐥𝐒𝐭𝐨𝐫𝐚𝐠𝐞 — are gifts from the browser, not the language. The browser quietly hands them off, runs them in the background, then places the result into a 𝐂𝐚𝐥𝐥𝐛𝐚𝐜𝐤 𝐐𝐮𝐞𝐮𝐞 . And here's the elegant part: The 𝐄𝐯𝐞𝐧𝐭 𝐋𝐨𝐨𝐩 sits there, watching. The moment the call stack is empty, it picks up the waiting callback functions and pushes it in. That's it. No magic. Just a disciplined handoff between three moving parts. JavaScript doesn't wait — but the browser builds the patience "around" it. 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲𝐬 : → 𝐓𝐡𝐞 𝐜𝐚𝐥𝐥 𝐬𝐭𝐚𝐜𝐤 𝐞𝐱𝐞𝐜𝐮𝐭𝐞𝐬 𝐟𝐚𝐬𝐭. 𝐍𝐞𝐯𝐞𝐫 𝐚𝐬𝐬𝐮𝐦𝐞 𝐢𝐭 𝐰𝐚𝐢𝐭𝐬. → 𝐖𝐞𝐛 𝐀𝐏𝐈𝐬 𝐚𝐫𝐞 𝐛𝐫𝐨𝐰𝐬𝐞𝐫-𝐩𝐨𝐰𝐞𝐫𝐞𝐝, 𝐚𝐜𝐜𝐞𝐬𝐬𝐞𝐝 𝐯𝐢𝐚 𝐭𝐡𝐞 𝐠𝐥𝐨𝐛𝐚𝐥 "𝐰𝐢𝐧𝐝𝐨𝐰" 𝐨𝐛𝐣𝐞𝐜𝐭. → 𝐓𝐡𝐞 𝐞𝐯𝐞𝐧𝐭 𝐥𝐨𝐨𝐩 𝐨𝐧𝐥𝐲 𝐚𝐜𝐭𝐬 𝐰𝐡𝐞𝐧 𝐭𝐡𝐞 𝐜𝐚𝐥𝐥 𝐬𝐭𝐚𝐜𝐤 𝐢𝐬 𝐜𝐥𝐞𝐚𝐫. #JavaScript #SoftwareEngineering #DeveloperJourney #LearningInPublic #Programming #TechCommunity #WebDevelopment
To view or add a comment, sign in
-
-
🧠 Ever wondered how JavaScript keeps track of which function is running? JavaScript uses something called the Call Stack. Think of it like a stack of tasks where functions are added and removed as they execute. 🔹 How the Call Stack Works JavaScript follows a Last In, First Out (LIFO) rule. That means: The last function added to the stack is the first one to finish. Example function first() { second(); } function second() { third(); } function third() { console.log("Hello from third function"); } first(); What happens in the Call Stack 1️⃣ first() is pushed to the stack 2️⃣ second() is called → pushed to the stack 3️⃣ third() is called → pushed to the stack 4️⃣ third() finishes → removed from stack 5️⃣ second() finishes → removed 6️⃣ first() finishes → removed 🔹 Visualising the Stack Call Stack at peak: - third() - second() - first() - Global() Then it unwinds back to the Global Execution Context. 💡 Why This Matters Understanding the call stack helps you understand: - Execution order - Stack overflow errors - Debugging JavaScript - Async behaviour It’s one of the core mechanics of the JavaScript engine. Next post: The Event Loop 🚀 #JavaScript #CallStack #Frontend #WebDevelopment #LearnJS #Programming #LearningInPublic
To view or add a comment, sign in
-
-
🤯 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭: 𝐖𝐡𝐞𝐫𝐞 𝐋𝐨𝐠𝐢𝐜 𝐆𝐨𝐞𝐬 𝐭𝐨 𝐃𝐢𝐞 (𝐚𝐧𝐝 𝐖𝐡𝐲) If you’ve ever looked at your console and thought, "That shouldn't be possible," you’re not alone. JavaScript is a language built on "best intentions" that often lead to total logic meltdowns. I’ve put together the attached guide (PDF) breaking down of the most infamous "WTFJS" moments—from why 0.1 + 0.2 isn't 0.3 to how the console can literally yell "banana" at you. 🔍 𝐖𝐡𝐚𝐭’𝐬 𝐚𝐜𝐭𝐮𝐚𝐥𝐥𝐲 𝐡𝐚𝐩𝐩𝐞𝐧𝐢𝐧𝐠 𝐮𝐧𝐝𝐞𝐫 𝐭𝐡𝐞 𝐡𝐨𝐨𝐝? Most of these "bugs" actually follow three very specific (and very weird) rules: • 𝐓𝐲𝐩𝐞 𝐂𝐨𝐞𝐫𝐜𝐢𝐨𝐧 (𝐓𝐡𝐞 𝐒𝐢𝐥𝐞𝐧𝐭 𝐊𝐢𝐥𝐥𝐞𝐫): JavaScript hates throwing errors. If you try to subtract a number from a string, it won't stop you—it will just "guess" what you meant. Sometimes it guesses wrong. • 𝐓𝐡𝐞 𝐈𝐄𝐄𝐄 𝟕𝟓𝟒 𝐒𝐭𝐚𝐧𝐝𝐚𝐫𝐝: JS doesn't store numbers the way humans write them. It uses binary floating-point math. When you deal with large integers or small decimals, you’re seeing the "rounding errors" of the machine. • 𝐓𝐫𝐮𝐭𝐡𝐲 𝐯𝐬. 𝐅𝐚𝐥𝐬𝐲: In JS, an empty array [] is technically "truthy," but when compared to a boolean using ==, the engine performs a series of hidden conversions that defy common sense. 🛡️ 𝐇𝐨𝐰 𝐭𝐨 𝐬𝐭𝐚𝐲 𝐬𝐚𝐧𝐞: • 𝐒𝐭𝐫𝐢𝐜𝐭 𝐄𝐪𝐮𝐚𝐥𝐢𝐭𝐲 (===): Never trust the double equals. • 𝐌𝐚𝐧𝐮𝐚𝐥 𝐂𝐚𝐬𝐭𝐢𝐧𝐠: Don't let JS guess your types; convert them yourself using Number() or String(). • 𝐔𝐬𝐞 𝐁𝐢𝐠𝐈𝐧𝐭: For those massive numbers that keep rounding up. Check out the PDF below for the full breakdown of these logic-defying snippets! Which of these tripped you up the most when you were starting out? Let’s swap horror stories in the comments. 👇 #Javascript #WebDevelopment #Programming #SoftwareEngineering #CodingLife #TechHumor
To view or add a comment, sign in
-
Confused between var, let, and const? Here's all you need to know. 👇 Most JavaScript developers use all three — but few know exactly when and why. Here's a quick breakdown: ⚠️ var → Function-scoped, hoists as undefined, can be re-declared. Avoid it in modern code. 🔁 let → Block-scoped, re-assignable. Use it when values change. 🔒 const → Block-scoped, immutable binding. Your default choice. 💡 TDZ (Temporal Dead Zone) — let and const are hoisted but can't be accessed before their declaration line. That's a feature, not a bug. 💥One rule of thumb: Always start with const. Switch to let only when you need to reassign. Never touch var. Save this for your next code review. 🔖 #JavaScript #WebDevelopment #Frontend #JS #Programming #100DaysOfCode #CodeTips #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Mastering JavaScript Array Methods Understanding array methods is a game-changer when writing clean, efficient JavaScript code. Here’s a quick breakdown of some essential ones: 🔹 map() – Transforms each element in an array 🔹 forEach() – Executes a function for every element 🔹 filter() – Selects elements based on a condition 🔹 push() & pop() – Add/remove elements from the end 🔹 shift() & unshift() – Add/remove elements from the beginning 🔹 reduce() – Combines elements into a single value These methods help simplify data manipulation and make your code more readable and powerful. Whether you're transforming data, filtering results, or aggregating values, knowing when to use each method can level up your JavaScript skills. 💡 Pro tip: Use map() for transformations and reduce() for calculations or summaries. #JavaScript #WebDevelopment #Coding #Frontend #Programming #DeveloperTips
To view or add a comment, sign in
-
-
🚀 Mastering JavaScript Array Methods Understanding array methods is a game-changer when writing clean, efficient JavaScript code. Here’s a quick breakdown of some essential ones: 🔹 map() – Transforms each element in an array 🔹 forEach() – Executes a function for every element 🔹 filter() – Selects elements based on a condition 🔹 push() & pop() – Add/remove elements from the end 🔹 shift() & unshift() – Add/remove elements from the beginning 🔹 reduce() – Combines elements into a single value These methods help simplify data manipulation and make your code more readable and powerful. Whether you're transforming data, filtering results, or aggregating values, knowing when to use each method can level up your JavaScript skills. 💡 Pro tip: Use map() for transformations and reduce() for calculations or summaries. #JavaScript #WebDevelopment #Coding #Frontend #Programming #DeveloperTips
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