Web Development Articles

Page 202 of 801

Finding the balance of brackets in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 412 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

Cutting off number at each digit to construct an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 164 Views

We need to write a JavaScript function that takes a number and returns an array of strings, where each string represents the number cut off at each digit position. Problem Given a number like 246, we want to create an array containing: First digit: "2" First two digits: "24" All digits: "246" Example Here's the implementation: const num = 246; const cutOffEach = (num = 1) => { const str = String(num); const res = []; let temp = ''; ...

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 203 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 two missing numbers that appears only once and twice respectively in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 250 Views

We need to write a JavaScript function that finds two numbers in an array where all other numbers appear three times, except one number that appears twice and another that appears only once. Problem Statement Given an array where most numbers appear three times, find the two numbers that appear exactly twice and exactly once respectively. Example Let's solve this step by step: const arr = [1, 1, 1, 2, 2, 3]; const findMissing = (arr = []) => { let x = 0; // number appearing once ...

Read More

Greatest number in a dynamically typed array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 182 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

Finding two prime numbers with a specific number gap in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 280 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

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

Finding value of a sequence for numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 633 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

Join arrays to form string in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 468 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 2011–2020 of 8,010 articles
« Prev 1 200 201 202 203 204 801 Next »
Advertisements