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
Object Oriented Programming Articles
Page 42 of 589
Get intersection between two ranges in JavaScript
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 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 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 MoreCompare keys & values in a JSON object when one object has extra keys in JavaScript
When comparing JSON objects in JavaScript, you often need to check if the common keys have matching values, even when one object has extra properties. This is useful for validating partial object matches or checking if one object is a subset of another. Problem Statement Consider these two objects: const obj1 = {a: "apple", b: "banana", c: "carrot"}; const obj2 = {a: "apple", e: "egg", b: "banana", c: "carrot", d: "dog"}; We need a function that returns true because all common keys (a, b, c) have matching values, ignoring the extra keys (e, d) ...
Read MoreRemove duplicates from array with URL values in JavaScript
Suppose, we have an array of objects like this − const arr = [ { url: 'www.example.com/hello', id: "22" }, { url: 'www.example.com/hello', id: "22" }, { url: 'www.example.com/hello-how-are-you', id: ...
Read MoreGenerating combinations from n arrays with m elements in JavaScript
We are required to write a JavaScript function that generates combinations from n number of arrays with m number of elements in them. For example, consider this data: const arr = [ [0, 1], [0, 1, 2, 3], [0, 1, 2] ] We have 3 sub-arrays with different numbers of elements. What we want to do is get all combinations by selecting one item from each array. Understanding the Problem The goal is to generate a Cartesian product of multiple arrays. For the example ...
Read MoreHow to create every combination possible for the contents of two arrays in JavaScript
Creating every possible combination from two arrays is known as a Cartesian product. This involves pairing each element from the first array with every element from the second array. Problem Setup Given two arrays of literals: const arr1 = ["A", "B", "C"]; const arr2 = ["1", "2", "3"]; console.log("Array 1:", arr1); console.log("Array 2:", arr2); Array 1: [ 'A', 'B', 'C' ] Array 2: [ '1', '2', '3' ] We need to create all possible combinations by pairing each element from the first array with each element from the second array. ...
Read MoreSeparating digits of a number in JavaScript
In JavaScript, separating digits of a number is a common programming task. This tutorial shows how to extract and display each digit of a number individually using different approaches. Problem Statement We need to create a program that takes a number as input and displays each digit separately. For example, if the input is 43354, the output should be: 4 3 3 5 4 Method 1: Using Mathematical Operations This approach uses the modulo operator (%) and integer division to extract digits from right to left: ...
Read MoreGet the total number of same objects in JavaScript
When working with arrays of objects in JavaScript, you often need to count how many objects share the same property values. This is useful for analyzing data like flight routes, user preferences, or categorizing items. Suppose we have an array of objects describing flight routes: const routes = [ { flyFrom: "CDG", flyTo: "DUB", return: 0, }, { ...
Read MoreSorting parts of array separately in JavaScript
We have an array that contains many objects. We are required to write a function to sort the first half of the array in ascending order and the second half of the array in ascending order as well, but without inter-mixing the entries of halves into one another. Consider this sample array: const arr = [ {id:1, x: 33}, {id:2, x: 22}, {id:3, x: 11}, {id:4, x: 3}, {id:5, x: 2}, {id:6, x: ...
Read More