Front End Technology Articles

Page 201 of 652

Finding the balance of brackets in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 420 Views

Given a string that consists of only two types of characters: "(" and ")". We are required to write a function that takes in one such string and balances the parentheses by inserting either a "(" or a ")" as many times as necessary. The function should then return the minimum number of insertions made in the string to balance it. For example: If the string is − const str = '()))'; Then the output should be 2, because by prepending '((' we can balance the string. How It Works The algorithm uses ...

Read More

Finding two prime numbers with a specific number gap in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 292 Views

Finding prime numbers with a specific gap is a common programming challenge. This involves identifying two prime numbers where their difference equals a given value within a specified range. Problem We need to write a JavaScript function that takes a gap number and a range array as arguments. The function should return the first pair of prime numbers that have an absolute difference equal to the gap and fall within the specified range. Algorithm Approach The solution involves two main steps: Find all prime numbers within the given range Search for consecutive primes with the specified gap ...

Read More

Sort array of objects by string property value in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 3K+ Views

The given task is to sort an array of objects by using string property value in JavaScript. Assume there is an array of objects and we need to sort those elements by using the string property. In this below scenario, we are sorting array of objects by "Company" property. const array = [ {Company: 'Oneplus', Manufacturing: 'China'}, {Company: 'Samsung', Manufacturing: 'South korea'}, {Company: 'Nothing', Manufacturing: 'India'} ]; // Output after sorting by "Company" property: [ {"Company":"Nothing", "Manufacturing":"India"}, {"Company":"Oneplus", "Manufacturing":"China"}, ...

Read More

Push specific elements to last in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 215 Views

Suppose we have an array of objects like this: const arr = [ {flag: true, other: 1}, {flag: true, other: 2}, {flag: false, other: 3}, {flag: true, other: 4}, {flag: true, other: 5}, {flag: true, other: 6}, {flag: false, other: 7} ]; We are required to write a JavaScript function that takes in one such array and sorts it based on the following conditions: If arr.flag === false, the matching ...

Read More

Finding value of a sequence for numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 642 Views

In JavaScript, we can calculate the value of mathematical sequences using loops and built-in Math functions. This article demonstrates how to implement a specific sequence formula that involves alternating signs, powers, and fractions. Problem Consider the following sequence sum: $$seq(n, \:p)=\displaystyle\sum\limits_{k=0}^{n}(-1)^{k}\times\:p\:\times 4^{n-k}\:\times\frac{2n-k}{k}$$ We need to write a JavaScript function that takes numbers n and p and returns the value of seq(n, p). The sequence includes alternating signs, exponential terms, and fractional components. Understanding the Formula The sequence contains several key components: (-1)^k - Creates alternating positive and negative terms p - A ...

Read More

Greatest number in a dynamically typed array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 186 Views

We are required to write a JavaScript function that takes in an array that contains some numbers, some strings and some false values. Our function should return the biggest Number from the array. For example: If the input array is − const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; Then the output should be 65. Using Math.max with Array Filtering The most straightforward approach is to filter valid numbers first, then use Math.max(): const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; ...

Read More

Constructing full name from first name, last name and optional middle name in JavaScript

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

We need to write a JavaScript function that takes in three strings: first name, last name, and an optional middle name, then returns the full name constructed from these inputs. Problem The challenge is handling the optional middle name parameter. If no middle name is provided, we should construct the full name with just the first and last names, avoiding extra spaces. Using Array Filter Method This approach uses an array to collect all name parts, filters out empty values, and joins them with spaces: const firstName = 'Vijay'; const lastName = 'Raj'; ...

Read More

Removing Negatives from Array in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 2K+ Views

In JavaScript, you often need to remove negative values from an array. This article shows three effective methods to accomplish this task. Input-Output Scenarios Let's look at typical scenarios. When an array contains both negative and positive values, we need to filter out the negatives: Input = [-2, 5, -7, 32, 78, -32] Output = [5, 32, 78] If the array contains only positive values, it remains unchanged: Input = [56, 43, 12, 67, 69, 34] Output = [56, 43, 12, 67, 69, 34] Using the filter() Method (Recommended) ...

Read More

Realtime moving average of an array of numbers in JavaScript

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

A moving average calculates the average of elements from the start of an array up to each position. For each element at index i, we compute the average of elements from index 0 to i. Problem We need to write a JavaScript function that takes an array of numbers and returns a new array containing the cumulative moving average at each position. Input: [1, 2, 3, 4, 5] Output: [1, 1.5, 2, 2.5, 3] The first element is the average of just the first element (1/1 = 1). The second element is the average ...

Read More

Join arrays to form string in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 474 Views

JavaScript provides built-in methods to join array elements into strings. The join() method converts a single array to string, while concat() combined with join() handles multiple arrays. Input-Output Scenarios Converting a single array to string: Input = [32, 45, 65, 12, 07, 55]; Output = "32, 45, 65, 12, 7, 55" // String Joining multiple arrays into a single string: Array1 = [123, 453, 656, 654, 125, 757]; Array2 = ["Hello", "honey", "bunny"]; Output = "123, 453, 656, 654, 125, 757, Hello, honey, bunny" // String Using Array.join() Method ...

Read More
Showing 2001–2010 of 6,519 articles
« Prev 1 199 200 201 202 203 652 Next »
Advertisements