Javascript Articles

Page 139 of 534

Swapcase function in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 601 Views

In JavaScript, a swapcase function converts uppercase letters to lowercase and lowercase letters to uppercase while leaving non-alphabetic characters unchanged. Problem Statement We need to create a function that takes a string and returns a new string where: Uppercase letters become lowercase Lowercase letters become uppercase Non-alphabetic characters remain unchanged Method 1: Using Helper Function This approach uses a separate function to determine the case of each character: const str = 'ThIs Is A STriNG'; const findLetter = (char = '') => { if (char.toLowerCase() === ...

Read More

Min window substring in JavaScript

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

We are required to write a JavaScript function that takes in two strings let's call them str1 and str2. The size of str1 is guaranteed to be greater than that of str2. We are required to find the smallest substring in str1 that contains all the characters contained in str2. For example − If the input strings are − const str1 = 'abcdefgh'; const str2 = 'gedcf'; Then the output should be − const output = 'cdefg'; because this the smallest consecutive substring of str1 that contains all characters ...

Read More

Finding the longest word in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 482 Views

We are required to write a JavaScript function that takes in a string as the only argument. The function should then iterate through the string and find and return the longest word from the string. For example − If the input string is − const str = 'Coding in JavaScript is really fun'; Then the output string should be − const output = 'JavaScript'; Using Array.reduce() Method The most efficient approach uses the reduce() method to iterate through words and track the longest one: const str = ...

Read More

Are mean mode of a dataset equal in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 219 Views

We are required to write a JavaScript function that takes in an array of numbers and checks if the mean and mode are equal. The function should calculate both statistical measures and return true if they match, false otherwise. For example − If the input array is − const arr = [5, 3, 3, 3, 1]; Then the output for this array should be true because both the mean and mode of this array are 3. Understanding Mean and Mode The mean is the average of all numbers in the dataset. The ...

Read More

Finding union of two sets in JavaScript

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

The union of two sets is the combination of all unique elements from both sets. In JavaScript, we can find the union of two arrays using several approaches. What is Union Set Union Set is the set made by combining the elements of two sets. Therefore, the union of sets A and B is the set of elements in A, or B, or both. For example − If we have two sets denoted by two arrays like this − const arr1 = [1, 2, 3]; const arr2 = [100, 2, 1, 10]; ...

Read More

Tribonacci series in JavaScript

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

The tribonacci sequence is a generalization of the Fibonacci sequence where each term is the sum of the three preceding terms. Unlike Fibonacci which uses two preceding terms, tribonacci uses three. The standard tribonacci series starts with 0, 1, 1, and each subsequent term is the sum of the previous three terms: 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149... How It Works Each term after the first three follows the formula: T(n) = T(n-1) + T(n-2) + T(n-3) Where T(0) = 0, T(1) = 1, T(2) = 1 ...

Read More

Finding n subarrays with equal sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 183 Views

We are required to write a JavaScript function that takes in an array of integers as the first argument and an integer as the second argument. The function should check whether we can create n (second argument) subarrays from the original array such that all the subarrays have the equal sum. For example − If the inputs are − const arr = [4, 3, 2, 3, 5, 2, 1]; const num = 4; The output should be true because the subarrays are: [5], [1, 4], [2, 3], [2, 3] all having sum equal ...

Read More

Total possible ways of making sum of odd even indices elements equal in array in nJavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 328 Views

We need to write a JavaScript function that counts how many elements can be removed from an array such that after removal, the sum of elements at odd indices equals the sum of elements at even indices. The approach involves calculating cumulative sums for odd and even positioned elements, then checking each removal scenario mathematically without actually removing elements. Problem Understanding Given an array, we want to find how many single element removals result in equal odd and even index sums in the remaining array. For example, with array [2, 6, 4, 2]: Remove index ...

Read More

Sorting array of exactly three unique repeating elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 342 Views

Suppose we have an array of Numbers that contains any frequency of exactly three elements: -1, 0 and 1 like this: const arr = [1, 1, 0, -1, 1, 0, -1, 1, 0, 0, 1]; console.log("Original array:", arr); Original array: [ 1, 1, 0, -1, 1, 0, -1, 1, 0, 0, 1 ] We are required to write a JavaScript function that takes in one such array. The function should simply sort this special array in place (without using any extra array to store the values). The only condition is that our function ...

Read More

Inserting a new interval in a sorted array of intervals in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 687 Views

For the purpose of this question, we define an interval as an array of two numbers where the first number is always smaller than the second number. For example − [4, 6], [2, 3], [6, 8], [2, 7], [1, 8] are all examples of valid intervals. Suppose, we have an array of intervals which is sorted according to their start times (the first elements of each interval). The intervals in the array are non-overlapping which means that for any two arbitrary adjacent intervals: [m, n], [x, y] m < n < x ...

Read More
Showing 1381–1390 of 5,340 articles
« Prev 1 137 138 139 140 141 534 Next »
Advertisements