Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 289 of 840
Create an object based on 2 others in JavaScript
In JavaScript, you often need to combine properties from multiple objects into a new one. There are several modern approaches to achieve this without modifying the original objects. Problem Statement Given two objects with properties and methods, we want to create a third object that contains all properties from both: const a = { a: 1, af: function() { console.log(this.a) }, }; const b = { b: 2, bf: function() { console.log(this.b) }, }; // Goal: Create object ...
Read MoreSearching for target string in a strangely sorted array in JavaScript
We are required to write a JavaScript function that takes in a word target and an array of sorted(by length(increasing), number of uppercase letters(decreasing), natural order) unique words which always contains the target. The task of our function is to find the index(0 based) of the target in the array of words, which would always be in the list. Understanding the Sorting Pattern The array is sorted by three criteria in order: Length: Shorter strings come first Uppercase count: Among same-length strings, more uppercase letters come first ...
Read MorePerforming power operations on an array of numbers in JavaScript
We need to write a JavaScript function that takes an array of integers with even length and performs a specific power operation to find two numbers whose squares sum to a calculated value. Problem Statement Given an array of integers with even length, we calculate: num = (arr[0]² + arr[1]²) × (arr[2]² + arr[3]²) × ... × (arr[n-2]² + arr[n-1]²) Our function should return an array [A, B] such that A² + B² = num. Example Walkthrough For array [1, 2, 3, 4]: First pair: 1² + 2² = 1 ...
Read MoreFind array elements that are out of order in JavaScript
Suppose we have an array of sorted numbers but some elements of the array are out of their sorted order. We are required to write a JavaScript function that takes in one such array and returns a subarray of all those elements that are out of order. Example The code for this will be − const arr = ["2", "3", "7", "4", "5", "6", "1"]; const findOutOfOrder = arr => { let notInOrder = []; notInOrder = arr.filter((el, ind) => { ...
Read MoreCheck for a self-dividing number in JavaScript
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 MoreData manipulation with JavaScript
When working with data arrays, you often need to combine different datasets. This tutorial shows how to merge a months array with a cashflows array to create a complete dataset with all months represented. Problem Statement Suppose we have two arrays describing cashflow data: const months = ["jan", "feb", "mar", "apr"]; const cashflows = [ {'month':'jan', 'value':10}, {'month':'mar', 'value':20} ]; console.log("Months:", months); console.log("Cashflows:", cashflows); Months: [ 'jan', 'feb', 'mar', 'apr' ] Cashflows: [ { month: 'jan', value: 10 }, { month: 'mar', value: 20 ...
Read MoreFinding reflection of a point relative to another point in JavaScript
Point reflection, also known as point symmetry, is a geometric transformation where a point P is reflected across a midpoint Q to create a new point P' that is equidistant from Q but in the opposite direction. P(6, -4) Q(11, 5) ...
Read MoreGroup by element in array JavaScript
Grouping array elements by a specific property is a common JavaScript task. This tutorial shows how to group an array of objects by their uuid property into separate sub-arrays. Problem Statement Given an array of objects with similar properties, we need to group them by a specific key and return an array of arrays. const arr = [ {"name": "toto", "uuid": 1111}, {"name": "tata", "uuid": 2222}, {"name": "titi", "uuid": 1111} ]; console.log("Original array:", arr); Original array: [ { name: ...
Read MorePalindrome numbers in JavaScript
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 MoreFind all disjointed intersections in a set of vertical line segments in JavaScript
We have a set of vertical regions defined by y1 and y2 coordinates, where y1 is the starting point and y2 is the ending point of each region. The origin of our coordinates system is the top-left corner, so y2 is always greater than y1. This is an example: const regions = [ [10, 100], [50, 120], [60, 180], [140, 220] ]; console.log(regions); [ [ 10, 100 ], [ 50, 120 ], [ 60, 180 ], [ ...
Read More