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 313 of 840
Substring combination in JavaScript
We are required to write a JavaScript function that takes in two strings as the first and the second argument. Let's call these strings str1 and str2. The function should check whether there exists a substring combination in str2, that when combined yields str2. By substring combination, we mean that we can skip characters but we have to maintain the order of the characters selected from str1. For example, if the input strings are: const str1 = 'desxooajmepwele'; const str2 = 'example'; Then the output should be true because the string 'example' can be ...
Read MoreIs the string a combination of repeated substrings in JavaScript
We need to write a JavaScript function that checks if a string can be constructed by repeating a substring multiple times. This is useful for pattern detection in strings. Problem Statement Given a string, determine if it consists of a repeated pattern. For example, "abcabcabc" is made by repeating "abc" three times, while "abcdef" has no repeating pattern. Example Input and Output For the string: const str = 'thisthisthisthis'; The expected output is: const output = true; Because the string is constructed by repeating 'this' four times. ...
Read MoreUnique pairs in array that forms palindrome words in JavaScript
We are required to write a JavaScript function that takes in an array of unique words. Our function should return an array of all such index pairs, the words at which, when combined yield a palindrome word. Problem Given an array of unique words, find all pairs of indices where concatenating the words at those indices creates a palindrome. A palindrome reads the same forwards and backwards, like "racecar" or "abccba". Example Following is the code: const arr = ["abcd", "dcba", "lls", "s", "sssll"]; const findPairs = (arr = []) => { ...
Read MoreHow to append new information and rethrowing errors in nested functions in JavaScript?
In JavaScript, appending new information to errors and rethrowing them in nested functions helps create more informative error messages while preserving the original error details. This technique is essential for debugging complex applications with multiple function layers. Understanding Error Rethrowing Error rethrowing allows us to catch an error, add contextual information, and throw it again to be handled at a higher level. This maintains the error chain while providing additional debugging context. Basic Error Rethrowing Here's how to rethrow an error with additional information: function innerFunction() { throw new Error("Original ...
Read MoreMap numbers to characters in JavaScript
When mapping numbers to characters in JavaScript, we need to convert digits to their corresponding alphabetical positions (1='a', 2='b', etc.). A number can be mapped in multiple ways by grouping digits differently. For example, the number 12145 can be interpreted as: 1, 2, 1, 4, 5 → a, b, a, d, e 12, 1, 4, 5 → l, a, d, e 12, 14, 5 → l, n, e However, combinations like 1, 2, 1, 45 are invalid because 45 exceeds the alphabet range (1-26). Solution Approach We use recursion to explore all valid digit ...
Read MoreMap multiple properties in array of objects to the same array JavaScript
When working with arrays of objects in JavaScript, you often need to extract multiple property values and combine them into a single flat array. This tutorial shows different approaches to map multiple properties from an array of objects. Problem Statement Given an array of objects like this: const arr = [ {a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6} ]; We need to extract all property values and create a flat array: const output = [1, 2, 3, 4, ...
Read MoreDetermining a pangram string in JavaScript
A pangram is a string that contains every letter of the English alphabet at least once. Examples include "The quick brown fox jumps over the lazy dog" and "Pack my box with five dozen liquor jugs". We need to write a JavaScript function that determines whether a given string is a pangram. For this problem, we'll focus on the 26 letters of the English alphabet, ignoring case sensitivity. How It Works The algorithm creates an array of all 26 letters, then iterates through the input string. For each letter found in the string, it removes that letter ...
Read MoreSorting string characters by frequency in JavaScript
In JavaScript, sorting string characters by frequency involves counting each character's occurrences and rearranging them from most frequent to least frequent. This technique is useful for data analysis, compression algorithms, and text processing tasks. Problem Statement We need to create a JavaScript function that takes a string as input and returns a new string where characters are arranged by their frequency in descending order. Characters appearing most frequently come first, followed by less frequent ones. For example, if the input string is: const str = 'free'; The expected output should be: ...
Read MoreSorting according to number of 1s in binary representation using JavaScript
Problem We need to write a JavaScript function that takes an array of numbers and sorts them in descending order based on the count of 1s in their binary representation. Understanding Binary Representation Every number has a binary representation. For example: 5 → 101 (two 1s) 78 → 1001110 (four 1s) 11 → 1011 (three 1s) Solution Approach We'll create two functions: one to count 1s in binary representation and another to sort the array. const arr = [5, 78, 11, 128, 124, 68, 6]; const countOnes = (num) => ...
Read MoreBalancing two arrays in JavaScript
We are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2, as the first and the second argument. The sum of elements in arr1 and arr2 are different. Our function should pick one element from the first array and push it in the second array and pick one element from the second array and push it in the first array such that the sum of the elements of both the arrays become equal. We should return an array of these two elements. Problem Statement For example, if the input to ...
Read More