Web Development Articles

Page 221 of 801

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 217 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 733 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 270 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 784 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

Group all the objects together having the same value for the '_id' key in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 405 Views

Suppose we have an array of objects like this − const arr = [ {_id : "1", S : "2"}, {_id : "1", M : "4"}, {_id : "2", M : "1"}, {_id : "" , M : "1"}, {_id : "3", S : "3"} ]; We are required to write a JavaScript function that takes in one such array and groups all the objects together that have the same value for the '_id' key. Therefore, the final output should ...

Read More

How to create a third object from two objects using the key values in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 255 Views

In JavaScript, you can create a third object from two objects by mapping key values between them. This is useful when you have one object containing arrays of keys and another object with corresponding values. Problem Statement Given two objects where the first contains arrays of keys and the second contains key-value pairs, we need to calculate totals for each category: const obj1 = { positive: ['happy', 'excited', 'joyful'], negative: ['depressed', 'sad', 'unhappy'] }; const obj2 = { happy: 6, excited: 1, unhappy: 3 }; ...

Read More

Get intersection between two ranges in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 894 Views

In JavaScript, finding the intersection between two numerical ranges involves determining the overlapping portion. A range is typically represented as an array with two elements: [start, end]. Understanding Range Intersection The intersection of two ranges is the overlapping segment. For ranges [2, 5] and [4, 7], the intersection is [4, 5] because that's where they overlap. const arr1 = [2, 5]; const arr2 = [4, 7]; console.log("Range 1:", arr1); console.log("Range 2:", arr2); Range 1: [ 2, 5 ] Range 2: [ 4, 7 ] Algorithm To find the intersection, we ...

Read More

How to add float numbers using JavaScript?

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

In JavaScript, floating-point numbers can be added directly using the addition operator (+). The parseFloat() function converts strings to floating-point numbers, but is not required when working with numeric literals. Example 1: Direct Addition The simplest way to add float numbers is using the addition operator directly: let inputFloat1 = 2.3; let inputFloat2 = 3.5; console.log("The two float values are:", inputFloat1, "and", inputFloat2); let result = inputFloat1 + inputFloat2; console.log("The sum of the float values is:", result); The two float values are: 2.3 and 3.5 The sum of the float values ...

Read More

Check for a self-dividing number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 300 Views

A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is self-dividing because it's divisible by 1, 2, and 8. However, any number containing 0 cannot be self-dividing since division by zero is undefined. What Makes a Number Self-Dividing? A number qualifies as self-dividing if: It contains no zeros (since division by zero is impossible) The number is evenly divisible by each of its individual digits Examples 128: Self-dividing because 128 ÷ 1 = 128, 128 ÷ 2 = ...

Read More

Palindrome numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 778 Views

We are required to write a JavaScript function that takes in a number and determines whether or not it is a palindrome number. Palindrome numbers − A palindrome number is that number which reads the same from both left and right sides. For example − 343 is a palindrome number 6789876 is a palindrome number 456764 is not a palindrome number Method 1: String Reversal Approach The simplest approach is to convert the number to a string, reverse it, and compare ...

Read More
Showing 2201–2210 of 8,010 articles
« Prev 1 219 220 221 222 223 801 Next »
Advertisements