AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 349 of 840

Computing the Cartesian Product of Two Sets in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 374 Views

In set theory, a Cartesian product is a mathematical operation that returns a set from multiple sets. For sets A and B, the Cartesian product A × B is the set of all ordered pairs (a, b) where a ∈ A and b ∈ B. We need to write a JavaScript function that takes two arrays representing distinct sets and returns a 2-D array containing their Cartesian product. What is Cartesian Product? The Cartesian product creates all possible combinations between elements of two sets. If set A has m elements and set B has n elements, the ...

Read More

Sorting string of words based on the number present in each word using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 672 Views

We are required to write a JavaScript function that takes in a string that represents a sentence. Our function should sort this sentence. Each word in the sentence string contains an integer. Our function should sort the string such that the word that contains the smallest integer is placed first and then in the increasing order. Problem Statement Given a string containing words with embedded numbers, we need to sort the words based on the numerical values they contain. For example, "is2 Thi1s T4est 3a" should become "Thi1s is2 3a T4est" because the numbers are in order ...

Read More

Checking for straight lines in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 553 Views

We need to write a JavaScript function that takes an array of coordinate pairs and determines if all points form a straight line. Each subarray contains exactly two items representing x and y coordinates. Our function checks whether the coordinates form a straight line by calculating and comparing slopes between points. For example: [[4, 5], [5, 6]] should return true (slope = 1) [[1, 2], [2, 3], [4, 5]] should return true (all have slope = 1) [[1, 1], [2, 2], [3, 4]] should return false (different slopes) The array is guaranteed to contain ...

Read More

Convert array with duplicate values to object with number of repeated occurrences in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 788 Views

When working with arrays that contain duplicate values, you often need to count occurrences and convert them into a structured format. This article shows how to transform an array with duplicates into an array of objects containing each unique value and its count. Problem Statement Suppose we have an array with duplicate entries like this: const arr = ['California', 'Texas', 'Texas', 'Texas', 'New York', 'Missouri', 'New Mexico', 'California']; console.log("Original array:", arr); Original array: [ 'California', 'Texas', 'Texas', 'Texas', 'New York', ...

Read More

Displaying likes on a post wherein array specifies the names of people that liked a particular post using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 323 Views

We need to write a JavaScript function that takes an array of names representing people who liked a post. The function should format the output based on the number of likes: show all names for 3 or fewer likes, or show the first two names plus the remaining count for more than 3 likes. Problem Statement Create a function that displays likes in a user-friendly format: 0 likes: "no one likes this" 1 like: "John likes this" 2 likes: "John and Mary like this" 3 likes: "John, Mary and Bob like this" 4+ likes: "John, Mary and ...

Read More

Frequency of elements of one array that appear in another array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 595 Views

We need to write a JavaScript function that takes two arrays of strings and returns the frequency count of each element from the second array as it appears in the first array. Problem Given two arrays, we want to count how many times each string from the second array appears in the first array. The result should be an array of counts corresponding to each element in the second array. Example Following is the code − const arr1 = ['abc', 'abc', 'xyz', 'cde', 'uvw']; const arr2 = ['abc', 'cde', 'uap']; const findFrequency = ...

Read More

Finding clusters of consecutive negative integers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 288 Views

We have an array of numbers like this: const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]; We are required to write a JavaScript function that counts the consecutive groups of negative numbers in the array. Understanding the Problem In the example array [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0], we have: First cluster: [-1, -2, -1] (positions 0-2) Second cluster: [-1, -2, -1, -2, -1] (positions 4-8) The function should return 2 since there are two separate groups of ...

Read More

Using JSON.stringify() to display spread operator result?

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

The spread operator (...) allows you to expand objects and arrays into individual elements. When combining objects with the spread operator, you can use JSON.stringify() to display the merged result as a formatted string. Syntax // Object spread syntax var result = { ...object1, ...object2 }; // Convert to JSON string JSON.stringify(result); Example Here's how to use the spread operator to merge objects and display the result: var details1 = { name: 'John', age: 21 }; var details2 = { countryName: 'US', subjectName: 'JavaScript' }; var result = { ...details1, ...details2 ...

Read More

Finding the power of a string from a string with repeated letters in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 593 Views

The power of a string is the maximum length of a non-empty substring that contains only one unique character. We are required to write a JavaScript function that takes in a string and returns its power. For example − const str = "abbcccddddeeeeedcba" Then the output should be 5, because the substring "eeeee" is of length 5 with the character 'e' only. How It Works The algorithm traverses the string and counts consecutive identical characters. It tracks the maximum count found, which represents the power of the string. ...

Read More

Filter nested object by keys using JavaScript

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

When working with arrays of objects in JavaScript, you often need to filter them based on specific criteria. This article demonstrates how to filter an array of objects by checking if their title property contains any of the specified search terms. Problem Statement Suppose we have an array of objects like this: const arr = [{ 'title': 'Hey', 'foo': 2, 'bar': 3 }, { 'title': 'Sup', 'foo': 3, 'bar': 4 }, { ...

Read More
Showing 3481–3490 of 8,392 articles
« Prev 1 347 348 349 350 351 840 Next »
Advertisements