AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 353 of 840

How to concatenate the string value length -1 in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 361 Views

In JavaScript, you can concatenate a string value (length-1) times using the Array() constructor with join(). This technique creates an array with empty elements and joins them with your desired string. Syntax new Array(count).join('string') Where count is the number of times you want to repeat the string, and the actual repetitions will be count - 1. How It Works When you create new Array(5), it generates an array with 5 empty slots. The join() method places the specified string between each element, resulting in 4 concatenated strings (length-1). Example var ...

Read More

Check if user inputted string is in the array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 533 Views

We need to write a JavaScript program that checks if a user-inputted string exists in a predefined array. The program should return true if the string is found, false otherwise. This is commonly needed when validating user input against a list of acceptable values, such as checking usernames, categories, or allowed options. Using Array.includes() Method The includes() method is the most straightforward way to check if an array contains a specific value. It returns a boolean result. Check ...

Read More

How to dynamically combine all provided arrays using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 503 Views

When working with arrays in JavaScript, you might need to generate all possible combinations from multiple arrays. This is commonly known as the Cartesian product of arrays. Suppose we have two arrays of literals like these: const arr1 = ['a', 'b', 'c']; const arr2 = ['d', 'e', 'f']; console.log('Array 1:', arr1); console.log('Array 2:', arr2); Array 1: [ 'a', 'b', 'c' ] Array 2: [ 'd', 'e', 'f' ] We need a JavaScript function that takes multiple arrays and builds all possible combinations from them. For these two arrays, we want to get ...

Read More

Square matrix rotation in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 645 Views

We are required to write a JavaScript function that takes in an array of arrays of n * n order (square matrix). The function should rotate the array by 90 degrees (clockwise). The condition is that we have to do this in place (without allocating any extra array). For example − If the input array is − const arr = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; Then the rotated array should look like − const output = [ ...

Read More

Weight sum of a nested array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 551 Views

We are required to write a JavaScript function that takes in a nested array, arr (nested up to any level) as the only argument. The function should calculate the weighted sum of the nested array and return that sum. For calculating weighted sum, we multiply each element with its level of nesting and add throughout the array. Elements at the first level are multiplied by 1, second level by 2, and so on. Problem Example For example, if the input to the function is: const arr = [4, 7, [6, 1, [5, 2]]]; ...

Read More

Reversing all alphabetic characters in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 181 Views

We are required to write a JavaScript function that takes in a string and reverses only the alphabetic characters, omitting all non-alphabetic characters from the result. Problem Given a string containing both alphabetic and non-alphabetic characters, we need to create a function that filters out non-alphabetic characters and returns the alphabetic characters in reverse order. Example Following is the code − const str = 'exa13mple'; function reverseLetter(str) { const res = str.split('') .reverse() .filter(val ...

Read More

Finding the character with longest consecutive repetitions in a string and its length using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 981 Views

We need to write a JavaScript function that finds the character with the longest consecutive repetitions in a string and returns both the character and its count as an array. Problem Given a string, we want to identify which character appears the most times consecutively and return an array containing the character and its consecutive count. Example Here's a solution that tracks consecutive characters and finds the maximum: const str = 'tdfdffddffsdsfffffsdsdsddddd'; const findConsecutiveCount = (str = '') => { if (!str) return ['', 0]; ...

Read More

Swapping adjacent words of a String in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 559 Views

In JavaScript, swapping adjacent words in a string means exchanging pairs of words - the first word with the second, the third with the fourth, and so on. If there's an odd number of words, the last word remains in its position. Example Here's how to swap adjacent words using the reduce method: const str = "This is a sample string only"; const replaceWords = str => { return str.split(" ").reduce((acc, val, ind, arr) => { if(ind % 2 === 1){ ...

Read More

Count by unique key in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 696 Views

When working with arrays of objects, you often need to count occurrences based on unique key values. This is particularly useful for data analysis, grouping records, or creating summary statistics. The Problem Given an array of objects with nested properties, we want to count how many times each unique user appears: const arr = [ { assigned_user: { name: 'Paul', ...

Read More

Finding all the unique paths in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 308 Views

In a grid navigation problem, we need to find the number of unique paths from the top-left corner (0, 0) to the bottom-right corner (m-1, n-1) of an m×n grid, where movement is restricted to only right or down directions. This is a classic dynamic programming problem. Each cell's value represents the number of ways to reach that position from the starting point. Algorithm Explanation The solution uses dynamic programming with these key principles: First row and first column have only 1 path each (straight line) For any other cell, paths = paths from above ...

Read More
Showing 3521–3530 of 8,392 articles
« Prev 1 351 352 353 354 355 840 Next »
Advertisements