Javascript Articles

Page 122 of 534

Get all methods of any object JavaScript

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

In JavaScript, you can extract all methods (functions) from any object using Object.getOwnPropertyNames() combined with type filtering. This is useful for debugging, reflection, or dynamically working with object methods. Understanding the Problem Objects contain both properties (data) and methods (functions). To get only the methods, we need to: Get all property names from the object Filter properties where the value type is "function" Return an array of method names Using Object.getOwnPropertyNames() The Object.getOwnPropertyNames() method returns an array of all properties (enumerable and non-enumerable) found directly on the given object. We then filter this ...

Read More

Longest distance between 1s in binary JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 457 Views

We are required to write a JavaScript function that takes a positive integer n and finds the longest distance between any two adjacent 1's in its binary representation. If there are no two adjacent 1's, the function returns 0. Two 1's are adjacent if there are only 0's separating them (possibly no 0's). The distance between two 1's is the absolute difference between their bit positions. Understanding the Problem For example, in the binary representation "1001", the two 1's have a distance of 3 (positions 0 and 3). Let's examine the number 22: ...

Read More

Finding degree of subarray in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 595 Views

The degree of an array is defined as the maximum frequency of any element in the array. Finding the shortest subarray with the same degree is a common programming problem that involves tracking element frequencies and their positions. const arr = [1, 2, 3, 3, 5, 6, 4, 3, 8, 3]; console.log("Array:", arr); console.log("Degree of array: 4 (element 3 appears 4 times)"); Array: [ 1, 2, 3, 3, 5, 6, 4, 3, 8, 3 ] Degree of array: 4 (element 3 appears 4 times) Our task is to find the length of the ...

Read More

Longest subarray which only contains strictly increasing numbers JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 335 Views

We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument. The function should then return the length of the longest continuous subarray from the array that only contains elements in a strictly increasing order. A strictly increasing sequence is the one in which any succeeding element is greater than all its preceding elements. Example const arr = [5, 7, 8, 12, 4, 56, 6, 54, 89]; const findLongest = (arr) => { if(arr.length == 0) { ...

Read More

Relative sorting in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 390 Views

Suppose, we have two arrays, let's say arr1 and arr2. The elements of arr2 are distinct, and all elements in arr2 are also in arr1. We are required to write a JavaScript function that takes in two such arrays and sorts the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order. For example− If the two input arrays are − const arr1 = [2, 3, 1, 3, 2, 4, 6, ...

Read More

Transform tree from DB format to JSON format in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 509 Views

When working with database records, data is often stored in a flat structure with parent-child relationships defined by IDs. This tutorial shows how to transform such flat data into a hierarchical tree structure in JavaScript. The Problem Suppose we have an array of objects representing geographical regions from a database: const arr = [ {"id":7, "name":"Kuwait", "parentId":2}, {"id":4, "name":"Iraq", "parentId":2}, {"id":10, "name":"Qatar", "parentId":2}, {"id":2, "name":"Middle East", "parentId":1}, {"id":3, "name":"Bahrain", "parentId":2}, {"id":6, "name":"Jordan", ...

Read More

Creating permutations to reach a target number, but reuse the provided numbers JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 207 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a target sum Number as the second argument. The function should return an array of all those subarrays from the original array whose elements sum to make the target sum. We can use a single number multiple times to achieve the sum. For example − If the input array and number are − const arr = [1, 2, 4]; const sum = 4; then the output should be − [ ...

Read More

Creating an array of objects based on another array of objects JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 729 Views

In JavaScript, you can create a new array of objects based on an existing array by transforming each object's structure. This is commonly done using the map() method to extract specific properties and create new object formats. Suppose we have an array of objects containing user data like this: const arr = [ {"user":"dan", "liked":"yes", "age":"22"}, {"user":"sarah", "liked":"no", "age":"21"}, {"user":"john", "liked":"yes", "age":"23"}, ]; console.log("Original array:", arr); Original array: [ { user: 'dan', liked: 'yes', age: '22' }, { ...

Read More

The algorithm problem - Backtracing pattern in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 267 Views

Backtracking is a powerful algorithmic pattern for solving constraint satisfaction problems. In this grid path problem, we need to find all possible paths from start to end that visit every walkable square exactly once. Problem Definition Given a 2D grid with different square types, find the number of unique paths from start to end: 1 represents the starting square (exactly one) 2 represents the ending square (exactly one) 0 represents empty squares we can walk over -1 represents obstacles we cannot walk over ...

Read More

Filter unique array values and sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 773 Views

In JavaScript, you may need to filter arrays that contain duplicate values and combine their numeric values. This is common when processing data like product orders, transactions, or inventory records. Problem Statement Given an array of arrays where each subarray has three elements [id, name, amount], we need to: Remove duplicate entries based on the first element (id) Sum the third element (amount) for matching entries const arr = [[12345, "product", "10"], [12345, "product", "15"], [1234567, "other", "10"]]; The expected output should combine the duplicate entries and sum their amounts: ...

Read More
Showing 1211–1220 of 5,340 articles
« Prev 1 120 121 122 123 124 534 Next »
Advertisements