Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 353 of 840
How to concatenate the string value length -1 in JavaScript.
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 MoreCheck if user inputted string is in the array in JavaScript
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 MoreHow to dynamically combine all provided arrays using JavaScript?
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 MoreSquare matrix rotation in JavaScript
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 MoreWeight sum of a nested array in JavaScript
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 MoreReversing all alphabetic characters in JavaScript
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 MoreFinding the character with longest consecutive repetitions in a string and its length using JavaScript
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 MoreSwapping adjacent words of a String in JavaScript
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 MoreCount by unique key in JavaScript
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 MoreFinding all the unique paths in JavaScript
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