AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 359 of 840

Swapcase function in JavaScript

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

Uncamelising a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 260 Views

We are required to write a JavaScript function that takes in a string as the first argument and a separator character as the second argument. The first string is guaranteed to be a camelCased string. The function should convert the case of the string by separating the words by the separator provided as the second argument. For example − If the input string is − const str = 'thisIsAString'; const separator = '_'; Then the output string should be − const output = 'this_is_a_string'; Using Custom Logic This ...

Read More

Evaluating a mathematical expression considering Operator Precedence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 586 Views

This article demonstrates how to evaluate mathematical expressions in JavaScript while respecting operator precedence. We'll implement a parser that handles the four basic arithmetic operators (+, -, *, /) and follows the standard order of operations. Problem Statement We need to write a JavaScript function that takes a mathematical expression as a string and returns its numerical result. The function must support: Addition (+) Subtraction (-) Multiplication (*) Division (/) as floating-point division The key challenge is handling operator precedence: multiplication and division must be evaluated ...

Read More

Compare and fill arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 288 Views

We are required to write a function that compares two arrays and creates a third array filling that array with all the elements of the second array and filling null for all those elements that are present in the first array but misses out in the second array. For example − If the two arrays are − const arr1 = ['f', 'g', 'h']; const arr2 = ['f', 'h']; Then the output should be − const output = ['f', null, 'h']; Algorithm Approach The solution uses an offset variable to ...

Read More

Trim and split string to form array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 918 Views

When working with comma-separated strings that contain unwanted whitespace, you need to trim spaces and split the string into an array. JavaScript provides multiple approaches to accomplish this task efficiently. Problem Statement Given a comma-separated string with irregular spacing: const str = "a, b, c, d , e"; console.log("Original string:", str); Original string: a, b, c, d , e We need to remove all whitespaces and split it into a clean array of elements. Method 1: Manual Space Removal This approach manually loops through the string to remove spaces ...

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 nth power of array element present at nth index using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 383 Views

We need to write a JavaScript function that takes an array of numbers and maps each element to its power based on its 0-based index position. For example, the element at index 0 is raised to power 0, the element at index 1 is raised to power 1, and so on. Problem Statement Create a function that transforms an input array where each element is raised to the power of its index position, then return the resulting array. Example Here's how to implement this using a traditional for loop: const arr = [5, 2, ...

Read More

Largest index difference with an increasing value in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 162 Views

We need to write a JavaScript function that finds the largest difference between two indices j - i where arr[i] ≤ arr[j]. This means we're looking for the maximum distance between two positions where the value at the later position is greater than or equal to the value at the earlier position. Problem Statement Given an array of numbers, find the maximum value of (j - i) such that arr[i] ≤ arr[j] and i < j. Example Let's implement a solution that checks all possible pairs: const arr = [1, 2, 3, 4]; ...

Read More

Calculating variance for an array of numbers in JavaScript

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

Problem We need to write a JavaScript function that calculates the variance for an array of numbers. Variance measures how spread out the numbers are from their average (mean). The variance formula involves two steps: Mean (M) = (Sum of all numbers) / (Count of numbers) Variance (V) = (Sum of squared differences from mean) / (Count of numbers) Understanding Variance Variance tells us how much the numbers in our dataset differ from the average. A low variance means numbers are close to the mean, while high variance means they're spread out. ...

Read More

Grouping array nested value while comparing 2 objects - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 465 Views

When working with complex nested objects, you often need to compare and group data from different states. This article demonstrates how to group array nested values while comparing two objects in JavaScript. Problem Statement Suppose we have a JSON object containing "before" and "after" states of device data: const input = { "before": { "device": [ { "id": "1234", "price": "10", ...

Read More
Showing 3581–3590 of 8,392 articles
« Prev 1 357 358 359 360 361 840 Next »
Advertisements