Javascript Articles

Page 64 of 534

Splitting a hyphen delimited string with negative numbers or range of numbers - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 511 Views

When working with hyphen-delimited strings containing negative numbers or ranges, simple split('-') won't work correctly because it treats every hyphen as a delimiter. We need regular expressions to distinguish between separating hyphens and negative number signs. The Problem Consider these strings with mixed content: var firstValue = "John-Smith-90-100-US"; var secondValue = "David-Miller--120-AUS"; Using split('-') would incorrectly split negative numbers and ranges, giving unwanted empty strings and broken values. Solution Using Regular Expression We use a lookahead pattern to split only at hyphens that precede letters or number ranges: var firstValue ...

Read More

Compare the Triplets - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 223 Views

The "Compare the Triplets" problem is a popular algorithmic challenge where you compare two arrays of three integers each and count how many comparisons each array wins. Alice and Bob each have three scores, and we need to determine who wins more individual comparisons. Problem Statement Given two triplets (arrays of 3 elements each), compare corresponding elements and award points: 1 point to Alice if her element is greater, 1 point to Bob if his element is greater, 0 points for ties. Return an array with [Alice's score, Bob's score]. Example Input Alice: [5, 6, ...

Read More

How to redundantly remove duplicate elements within an array – JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 313 Views

Let's say we have an array with duplicate elements like this: [10, 20, 10, 50, 60, 10, 20, 40, 50] JavaScript provides several methods to remove duplicate elements from arrays. The most common and efficient approach is using the Set object with the spread operator. Using Set with Spread Operator (Recommended) The Set object stores only unique values. Combined with the spread operator, it creates a new array without duplicates: var originalArray = [10, 20, 10, 50, 60, 10, 20, 40, 50]; var arrayWithNoDuplicates = [...new Set(originalArray)]; console.log("Original array:", originalArray); console.log("No ...

Read More

How to replace before first forward slash - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 871 Views

Let's say the following is our string with forward slash — var queryStringValue = "welcome/name/john/age/32" To replace before first forward slash, use replace() along with regular expressions. Syntax string.replace(/^[^/]+/, "replacement") Example Following is the code — var regularExpression = /^[^/]+/ var queryStringValue = "welcome/name/john/age/32" var replacedValue = queryStringValue.replace(regularExpression, 'index'); console.log("Original value=" + queryStringValue); console.log("After replacing the value=" + replacedValue); Original value=welcome/name/john/age/32 After replacing the value=index/name/john/age/32 How the Regular Expression Works The regular expression /^[^/]+/ breaks down as follows: ^ ...

Read More

Convert number to a reversed array of digits in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 217 Views

Given a non-negative integer, we need to write a function that returns an array containing the digits in reverse order. This is a common programming challenge that demonstrates string manipulation and array operations. Example 348597 => The correct solution should be [7, 9, 5, 8, 4, 3] Method 1: Using String Split and Reverse The most straightforward approach is to convert the number to a string, split it into individual characters, reverse the array, and convert back to numbers: const num = 348597; const reverseArrify = num => { ...

Read More

JavaScript Get English count number

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 246 Views

We are required to write a JavaScript function that takes in a number and returns an English ordinal number for it (1st, 2nd, 3rd, 4th, etc.). Example 3 returns 3rd 21 returns 21st 102 returns 102nd How Ordinal Numbers Work English ordinal numbers follow these rules: Numbers ending in 1: add "st" (1st, 21st, 31st) — except 11th Numbers ending in 2: add "nd" (2nd, 22nd, 32nd) — except 12th Numbers ending in 3: add "rd" (3rd, 23rd, 33rd) — except 13th All others: add "th" (4th, 5th, 6th, 7th, 8th, 9th, ...

Read More

Finding smallest number using recursion in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 232 Views

We are required to write a JavaScript function that takes in an array of Numbers and returns the smallest number from it using recursion. Let's say the following are our arrays: const arr1 = [-2, -3, -4, -5, -6, -7, -8]; const arr2 = [-2, 5, 3, 0]; Recursive Approach The recursive solution uses a helper function that compares the first element with the rest of the array: const arr1 = [-2, -3, -4, -5, -6, -7, -8]; const arr2 = [-2, 5, 3, 0]; const min = arr => { ...

Read More

Subarrays product sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 179 Views

We are required to write a JavaScript function that takes in an array of numbers of length N such that N is a positive even integer and divides the array into two sub arrays (say, left and right) containing N/2 elements each. The function should calculate the product of each subarray and then add both the results together. Example If the input array is: const arr = [1, 2, 3, 4, 5, 6] The calculation would be: (1*2*3) + (4*5*6) 6 + 120 126 Using Array.reduce() Method Here's ...

Read More

JavaScript Strings: Replacing i with 1 and o with 0

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 276 Views

We are required to write a function that takes in a string as one and only argument and returns another string that has all 'i' and 'o' replaced with '1' and '0' respectively. It's one of those classic for loop problems where we iterate over the string with its index and construct a new string as we move through. Using For Loop The most straightforward approach is to iterate through each character and build a new string: const string = 'Hello, is it raining in Amsterdam?'; const replaceChars = (str) => { ...

Read More

Add line break inside a string conditionally in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

In JavaScript, you can add line breaks inside a string conditionally by checking character limits and replacing spaces with newline characters. This is useful for formatting text within specific width constraints. Problem Description We need to create a function breakString() that takes two parameters: a string to be broken and a threshold number representing the maximum characters per line. When the character count exceeds this limit and we encounter a space, we replace it with a line break. Algorithm Approach The solution iterates through the string while maintaining a character count. When the count reaches the ...

Read More
Showing 631–640 of 5,340 articles
« Prev 1 62 63 64 65 66 534 Next »
Advertisements