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 58 of 589
Computing the Cartesian Product of Two Sets in JavaScript
In set theory, a Cartesian product is a mathematical operation that returns a set from multiple sets. For sets A and B, the Cartesian product A × B is the set of all ordered pairs (a, b) where a ∈ A and b ∈ B. We need to write a JavaScript function that takes two arrays representing distinct sets and returns a 2-D array containing their Cartesian product. What is Cartesian Product? The Cartesian product creates all possible combinations between elements of two sets. If set A has m elements and set B has n elements, the ...
Read MoreHow do I change a tag\'s inner HTML based on their order in JavaScript?
In JavaScript, you can change a tag's innerHTML based on their order using document.querySelectorAll() or getElementsByClassName(). These methods return collections of elements that you can iterate through and modify based on their position in the DOM. This technique is useful for numbering elements, adding order-based content, or applying different innerHTML to similar elements based on their sequence. Using document.querySelectorAll() The querySelectorAll() method returns a static NodeList containing all elements that match the specified CSS selector(s). Syntax document.querySelectorAll(selectors) Example 1: Basic Order Assignment Here's how to change innerHTML based on element order ...
Read MoreAdding a unique id for each entry in JSON object in JavaScript
In this problem statement, our task is to add a unique id for every object entry in a JSON object with the help of JavaScript. We will use a loop and a variable to store the id for each object in the JSON object. Understanding the Problem Statement The problem statement is to write a function in JavaScript that adds a unique id to every item in the given JSON object. For example, if we have: let obj = { "entry1": {empName: "A", age: 25}, "entry2": {empName: "B", age: 30} ...
Read MoreGet all substrings of a string in JavaScript recursively
In JavaScript, generating all substrings of a string can be elegantly solved using recursion. A substring is any continuous sequence of characters within a string. For example, the string "abc" has substrings: "a", "b", "c", "ab", "bc", and "abc". Understanding the Problem We need to create a recursive function that generates all possible substrings of a given input string. For the string "xy", the possible substrings are "x", "y", and "xy". The recursive approach systematically explores all starting and ending positions to build each substring. Algorithm Steps Step 1 − Define the main function that accepts ...
Read MoreSorting odd and even elements separately JavaScript
In JavaScript, we can sort odd and even positioned elements separately within an array while maintaining their relative positions. This approach sorts elements at even indices (0, 2, 4...) and odd indices (1, 3, 5...) independently. Understanding the Problem Given an array, we need to sort elements at even indices separately from elements at odd indices. For example, with array [9, 2, 7, 4, 5, 6, 3, 8, 1]: Even indices (0, 2, 4, 6, 8): [9, 7, 5, 3, 1] → sorted: [1, 3, 5, 7, 9] Odd indices (1, 3, 5, 7): [2, 4, ...
Read MoreAlgorithm to add binary arrays in JavaScript
In this problem statement, our target is to add two binary arrays with the help of JavaScript. So we will break down the problem step by step to ensure understanding. What is a Binary Array? A binary array is an array that represents a binary number. In a binary number system we have only two possible digits, 0 and 1. The binary array will have elements which can only be 0s and 1s. Every item in the array represents a bit of the binary number. For example, we have a binary array [1, 0, 1, 0]. This ...
Read MoreAverage with the Reduce Method in JavaScript
In JavaScript, calculating the average of array elements using the reduce() method is an efficient and elegant approach. The reduce() method iterates through the array once to compute the sum, which we then divide by the array length. Understanding the Problem We need to calculate the average value of all elements in an array using JavaScript's reduce() method. For example, given the array [1, 2, 3, 4, 5], the average would be (1 + 2 + 3 + 4 + 5) / 5 = 15 / 5 = 3. Algorithm Step 1: Create a function that ...
Read MoreCheck if string ends with desired character in JavaScript
In JavaScript, you can check if a string ends with a specific character using several built-in methods. The most common approaches are using endsWith(), charAt(), or bracket notation. Understanding the Problem We need to determine if a given string ends with a specific character. For example, checking if "Hello World!" ends with "!" should return true, while checking if "Hello World." ends with "!" should return false. Using endsWith() Method (Recommended) The endsWith() method is the most straightforward approach: const str1 = "Hello, Tutorials Point!"; const str2 = "Hello, World."; const desiredChar = "!"; ...
Read MoreAbsolute Values Sum Minimization in JavaScript
In JavaScript, finding the value that minimizes the sum of absolute differences from all elements in an array is a fundamental optimization problem. The optimal solution is to find the median of the array. Understanding the Problem Given an array of numbers, we need to find a value x that minimizes the sum of |arr[i] - x| for all elements in the array. Mathematically, this optimal value is always the median of the sorted array. Sum Minimization Concept 1 ...
Read MoreCheck whether we can form string2 by deleting some characters from string1 without reordering the characters of any string - JavaScript
In this problem, we need to determine whether we can form string2 by removing some characters from string1 without changing the order of characters in either string. This is essentially checking if string2 is a subsequence of string1. Understanding the Problem We want to check if the second string can be formed from the first string while preserving character order. Let's look at some examples: // Example 1: Can form "ale" from "apple" let s1 = "apple"; let s2 = "ale"; console.log("Can form '" + s2 + "' from '" + s1 + "'?"); // Expected: ...
Read More