AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 334 of 840

Constructing a string based on character matrix and number array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 258 Views

We need to write a JavaScript function that takes an n × n matrix of string characters and an array of integers (positive and unique), then constructs a string from characters at the specified 1-based positions. Problem Understanding Given a character matrix, we flatten it into a single array and extract characters at positions specified by the number array (using 1-based indexing). Character Matrix: [ ['a', 'b', 'c', 'd'], ['o', 'f', 'r', 'g'], ['h', 'i', 'e', 'j'], ['k', 'l', 'm', 'n'] ] ...

Read More

Finding the most frequent word(s) in an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 969 Views

In JavaScript, finding the most frequent words in an array is a common problem that involves counting occurrences and sorting by frequency. This article demonstrates how to find the top N most frequent words from an array of strings. Problem Statement We need to write a JavaScript function that takes an array of lowercase English strings and returns the most frequent elements. The function should: Accept an array of strings as the first parameter Accept a number specifying how many top frequent words to return Sort results by frequency (highest to lowest) For words with equal ...

Read More

How many numbers in the given array are less/equal to the given value using the percentile formula in Javascript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 242 Views

In this article, you will understand how to calculate the percentile of a given value in an array using JavaScript. The percentile tells us what percentage of numbers in the array are less than or equal to a specific value. Percentile Formula We use the following formula to calculate the percentile: Percentile = (n/N) * 100 Where: n = count of values less than or equal to the given value N = total number of values in the array For values equal to our target, ...

Read More

Finding pandigital numbers using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 249 Views

A pandigital number is a number that contains all digits (0-9) at least once. In this tutorial, we'll create a JavaScript function to check if a given number string is pandigital. What is a Pandigital Number? A pandigital number must contain every digit from 0 to 9 at least once. For example, "1234567890" is pandigital, while "123456789" is not (missing 0). Example Let's implement a function to check if a number string is pandigital: const numStr1 = '47458892414'; const numStr2 = '53657687691428890'; const isPandigital = numStr => { let ...

Read More

How to convert square bracket object keys into nested object in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 836 Views

In JavaScript, objects with square bracket notation in their keys can be converted into properly nested objects. This is useful when dealing with flat data structures that represent nested relationships. Consider an object with square bracket notation: const flatObj = { "object[foo][bar][ya]": 100 }; console.log("Original object:", flatObj); Original object: { 'object[foo][bar][ya]': 100 } We want to convert this into a nested structure where each bracket represents a deeper level of nesting. Understanding the Problem The goal is to transform a key like "object[foo][bar][ya]" into a nested object structure: ...

Read More

Checking if an array is sorted lexicographically in reference to some scrambled alphabet sequence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 363 Views

We need to write a JavaScript function that checks if an array of words is sorted lexicographically according to a custom alphabet order. The function takes two arguments: an array of words and a string representing the custom alphabetical order. The task is to verify whether the words are arranged correctly based on the given alphabet sequence. If they are properly sorted, return true; otherwise, return false. Problem Understanding Consider this example: const arr = ['this', 'is', 'something', 'mad']; const order = 'hdetljnopqabcuvwxfgirsykmz'; In the custom order, 't' comes before 'i', 'i' comes ...

Read More

Building an array of specific size with consecutive element sum being perfect square in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 201 Views

We need to create a JavaScript function that arranges numbers 1 to n such that the sum of each pair of consecutive elements forms a perfect square. This is a classic backtracking problem that requires careful arrangement of numbers. Problem Understanding For an array of size n, we must arrange integers 1 through n where each adjacent pair sums to a perfect square. For example, if two consecutive numbers are 9 and 7, their sum (16) should be a perfect square (4²). Algorithm Approach We use a backtracking algorithm with these steps: Try each ...

Read More

Limiting string up to a specified length in JavaScript

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

Truncating strings to a specified length is a common requirement in JavaScript applications, especially for displaying previews or fitting text within UI constraints. This article shows how to limit string length and append ellipsis when truncation occurs. Problem We need to write a JavaScript function that takes a string and a number. The function should return the truncated version of the string up to the given limit followed by "..." if the result is shorter than the original string. Otherwise, it should return the same string if nothing was truncated. Using String.slice() Method The most straightforward ...

Read More

Can one string be repeated to form other in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 157 Views

We are required to write a JavaScript function that takes in two strings, str1 and str2, as the first and the second argument. Our function should return the minimum number of times we should repeat string str1 so that string str2 is a substring of it. If it is impossible for str2 to be a substring after repeating it, we should return -1. Problem Example For example, if the input to the function is: const str1 = 'wxyz'; const str2 = 'yzwxyzwx'; The expected output is 3, because by repeating str1 three times ...

Read More

Comparing objects in JavaScript and return array of common keys having common values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 832 Views

We are required to write a JavaScript function that takes in two objects. The function should return an array of all those common keys that have common values across both objects. Example The code for this will be − const obj1 = { a: true, b: false, c: "foo" }; const obj2 = { a: false, b: false, c: "foo" }; const compareObjects = (obj1 = {}, obj2 = {}) => { const common = Object.keys(obj1).filter(key => { if(obj1[key] === obj2[key] && obj2.hasOwnProperty(key)){ ...

Read More
Showing 3331–3340 of 8,392 articles
« Prev 1 332 333 334 335 336 840 Next »
Advertisements