Day 15 / 30 - Javascript Coding practice Problem : Longest Common Prefix Find the longest common prefix among an array of strings. If there’s no common prefix, return an empty string "". Solution: var longestCommonPrefix = function (strs) { let longestStr = strs[0] for (let k = 1; k < strs.length; k++) { let j = 0; while (j < strs[k].length && j < longestStr.length && strs[k][j] === longestStr[j]) { j++; } longestStr = longestStr.slice(0, j) if (longestStr === "") return "" } return longestStr; }; #JavaScript #DSA #CodingPractice #100DaysOfCode #ProblemSolving #FrontendDevelopment
Longest Common Prefix in JavaScript
More Relevant Posts
-
Day 9 / 30 - Javascript Coding Problem: Given an integer array nums, a reducer function fn, and an initial value init, return the final result obtained by executing the fn function on each element of the array, sequentially, passing in the return value from the calculation on the preceding element. This result is achieved through the following operations: val = fn(init, nums[0]), val = fn(val, nums[1]), val = fn(val, nums[2]), ... until every element in the array has been processed. The ultimate value of val is then returned. If the length of the array is 0, the function should return init. Please solve it without using the built-in Array.reduce method. Solution: var reduce = function (nums, fn, init) { let numsLength = nums?.length; if (numsLength === 0) { return init; } let acc = init; for (let k = 0; k < numsLength; k++) { acc = fn(acc, nums[k]); } return acc; }; #JavaScript #DSA #CodingPractice #100DaysOfCode #FrontendDevelopment #ProblemSolving
To view or add a comment, sign in
-
-
Work in progress 💻 Deep in the code today, building out new functionality for Scrimba Advance JavaScript. Sometimes the best commits are the ones that say "promise" twice because that's exactly what clean, asynchronous code delivers. Those small, focused commits? That's where the real progress happens. 𝖶𝖺𝗇𝗍 𝗍𝗈 𝗅𝖾𝖺𝗋𝗇 coding click 𝗁𝖾𝗋𝖾 👉🏽 : https://shorturl.at/cESup #WebDevelopment #JavaScript #Coding
To view or add a comment, sign in
-
-
Day 6 / 30 - JavaScript Coding Practice Today’s problem: Roman to Integer var romanToInt = function (s) { if (s.length > 15) { return; } const romanMap = new Map([ ['I', 1], ['V', 5], ['X', 10], ['L', 50], ['C', 100], ['D', 500], ['M', 1000] ]); let sum = 0; let preValue = 0; for (let k = s.length - 1; k >= 0; k--) { let currentVal = romanMap.get(s[k]); if (currentVal < preValue) { sum -= currentVal; } else { sum += currentVal; } preValue = currentVal; } return sum; }; 💡 Key Insight: Instead of scanning from left to right, I iterated from right to left to easily handle subtraction cases like IV (4) and IX (9). 👉 If a smaller value comes before a larger one → subtract 👉 Otherwise → add #JavaScript #DSA #CodingPractice #ProblemSolving #100DaysOfCode #FrontendDevelopment
To view or add a comment, sign in
-
-
🚀 Learning JavaScript? Start with Strings. Strings are one of the most used things in JavaScript. If you can work with text, you can build forms, messages, search features, and much more. Let’s understand the basics 👇 • Create a string using quotes let name = "JavaScript"; • Find string length name.length • Join strings together "Hello " + "World" • Change text case name.toUpperCase() or name.toLowerCase() • Get part of a string name.substring(0,4) Small concept… but used everywhere in real projects. Master the basics → coding becomes easier. #JavaScript #WebDevelopment #FrontendDevelopment #LearnToCode #ProgrammingBasics #JavaScriptTips #CodingForBeginners #DeveloperCommunity #TechEducation #SoftwareDevelopment
To view or add a comment, sign in
-
-
I used to get confused between Spread and Rest in JavaScript. Same ... syntax… but different behavior. That’s where most mistakes happen. The trick is simple: 👉 Spread = expand values 👉 Rest = collect values Once that clicked, writing cleaner code became much easier. 🔗 Read here: https://lnkd.in/d7B7MJdF #javascript #webdevelopment #coding
To view or add a comment, sign in
-
-
A classic JavaScript quirk that often trips developers up: why does typeof null equal "object"? This behavior dates back to the very early days of JavaScript. It's a known inconsistency, not a bug in the sense of broken functionality, but certainly a surprising one. Understanding this nuance is crucial for writing robust JavaScript. Always remember that checking `value === null` is the reliable way to test for a null value, rather than relying on `typeof`. Mastering these JavaScript oddities helps us build more predictable and maintainable code. #JavaScript #Programming #WebDevelopment #DeveloperTips
To view or add a comment, sign in
-
-
Day 10 / 30 - Javascript Coding Challenge Problem: Given an array of functions [f1, f2, f3, ..., fn], return a new function fn that is the function composition of the array of functions. The function composition of [f(x), g(x), h(x)] is fn(x) = f(g(h(x))). The function composition of an empty list of functions is the identity function f(x) = x. You may assume each function in the array accepts one integer as input and returns one integer as output. Solution: var compose = function (functions) { return function (x) { let sum = x; for (let k = functions.length - 1; k >= 0; k--) { sum = functions[k](sum) } return sum } }; #JavaScript #FunctionalProgramming #DSA #CodingPractice #100DaysOfCode #FrontendDevelopment
To view or add a comment, sign in
-
-
🚀 Unleash the power of asynchronous programming in JavaScript! Learn how to use Promises to handle async operations like a pro. 🌟 For developers, understanding Promises is crucial for writing efficient and responsive code. They help manage asynchronous tasks and avoid callback hell, making your code more readable and maintainable. Now, let's dive into the steps of utilizing Promises: 1. Create a new Promise object using the `new Promise()` constructor. 2. Inside the Promise, define the async task logic using the resolve and reject functions. 3. Use `.then()` to handle the resolved Promise and `.catch()` for any errors encountered. 👨💻 Pro Tip: Chain multiple `.then()` methods for sequential async operations. 🚫 Common Mistake: Forgetting to handle Promise rejections, leading to uncaught errors. What kind of async tasks do you find most challenging to handle with Promises? 🤔💡 🌐 View my full portfolio and more dev resources at tharindunipun.lk #JavaScript #Promises #AsyncProgramming #WebDevelopment #FrontEnd #CodingTips #DeveloperCommunity #LearnToCode
To view or add a comment, sign in
-
-
If you want to master asynchronous programming with JavaScript, you must be very comfortable with promises. But to master promises, you must understand callbacks deeply. But how can one do that? Well, don't worry, I've got something for you. I wrote a blog on it. And not just to show callbacks but to help you internalise it. I am confident about this because I have been in a place where I felt like I could never understand callbacks. But now that I have got the feel for it, I just don't want to keep it to myself. It is for everyone. So please check out my blog. All I ask you is just for a review on how the read was so that I can improve on it. Here is the link to my blog. Callbacks In JavaScript : https://lnkd.in/dXUVK--5
To view or add a comment, sign in
-
-
Just published a new blog on Template Literals in JavaScript! If you're still using old string concatenation, you're missing out on cleaner and more powerful ways to write code. In this blog, I’ve explained how template literals make your JavaScript more readable and dynamic with real examples. 💡 Learn how to: Embed expressions easily Write multi-line strings effortlessly Improve code readability Check it out here 👇 https://lnkd.in/dXZMq4RW #JavaScript #WebDevelopment #Coding #Frontend #100DaysOfCode
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