Front End Technology Articles

Page 186 of 652

Array thirds with equal sums in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 188 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument. Our function should return true if and only if we can partition the array into three non-empty parts with equal sums, false otherwise. Problem Statement Given an array of integers, determine if it can be split into exactly three contiguous subarrays with equal sums. For example, if the input to the function is: const arr = [3, 3, 6, 5, -2, 2, 5, 1, -9, 4]; Then the output should be: ...

Read More

All combinations of sums for array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 922 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n as the second argument. The number n will always be less than or equal to the length of the array. Our function should return an array of the sum of all elements of all the possible subarrays of length n from the original array. For example, If the input is: const arr = [2, 6, 4]; const n = 2; Then the output should be: const output = ...

Read More

Finding intersection of arrays of intervals in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 601 Views

JavaScript function that takes in two arrays, arr1 and arr2 of intervals which are pairwise disjoint and in sorted order. A closed interval [a, b] (with a

Read More

Dynamic Programming: return all matched data in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 377 Views

Dynamic programming in JavaScript can be used to efficiently search and retrieve data from nested structures. This article demonstrates how to search for all matching cities in a complex JSON object containing country and province information. The Data Structure We have a nested JSON object with countries, provinces, and cities: const countryInfo = { country: [{ name: "Bangladesh", province: [{ name:"Dhaka", ...

Read More

Calculating quarterly and yearly average through JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 330 Views

In this tutorial, we'll learn how to calculate quarterly and yearly averages from an array of numbers using JavaScript. This involves chunking data into groups and computing their averages. Suppose we have an array of numbers like this: const arr = [1, 2, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; console.log("Original array:", arr); Original array: [1, 2, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] We need to group this array ...

Read More

Finding squares in sorted order in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 318 Views

We are required to write a JavaScript function that takes in an array of integers, arr, sorted in increasing order. Our function is supposed to return an array of the squares of each number, also sorted in increasing order. For example, if the input to the function is − const arr = [-2, -1, 1, 3, 6, 8]; Then the output should be − const output = [1, 1, 4, 9, 36, 64]; The Challenge Simply squaring and sorting would work, but it's inefficient. Since the original array is ...

Read More

Group strings starting with similar number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 327 Views

In JavaScript, you can group strings that start with the same number by extracting the first part before the decimal and comparing consecutive elements. This is useful when working with version numbers, decimal classifications, or any structured string data. Problem Statement Given an array of number strings like this: const arr = ["1.1", "1.2", "1.3", "2.1", "2.2", "3.1", "3.2", "3.3", "4.1", "4.2"]; console.log("Input array:", arr); Input array: [ '1.1', '1.2', '1.3', '2.1', '2.2', '3.1', '3.2', '3.3', '4.1', '4.2' ] We need to group all strings starting with the same number into ...

Read More

Checking for centrally peaked arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 161 Views

We are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument. Our function should check whether the input array is a centrally peaked array or not. If it is a centrally peaked array, we should return true, false otherwise. Conditions for Centrally Peaked Array The conditions for being a centrally peaked array are: arr.length >= 3 There exists some i with 0 < i < arr.length - 1 such that: ...

Read More

Replace all occurrence of specific words in a sentence based on an array of words in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 582 Views

We are required to write a JavaScript function that takes a string and an array of strings. Our function should return a new string, where all the occurrences of the word in the string that are present in the array are replaced by a whitespace. Our function should use the String.prototype.replace() method to solve this problem. Understanding the Problem When filtering words from a sentence, we need to: Match whole words only (not parts of words) Handle case-insensitive matching Use regular expressions ...

Read More

Checking for a Doubleton Number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 372 Views

A "doubleton number" is a natural number that contains exactly two distinct digits. For example, 23, 35, 100, and 12121 are doubleton numbers because they each use only two different digits. Numbers like 123 (three digits) and 9980 (three digits: 9, 8, 0) are not doubleton numbers. Problem Statement We need to write a JavaScript function that takes a number and returns true if it's a doubleton number, false otherwise. Solution The approach is to convert the number to a string, track unique digits using an object, and check if exactly two distinct digits exist. ...

Read More
Showing 1851–1860 of 6,519 articles
« Prev 1 184 185 186 187 188 652 Next »
Advertisements