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 26 of 589
Sort an array according to another array in JavaScript
When working with arrays in JavaScript, you may need to sort one array based on the order of elements in another array. This is useful when you have a reference array that defines the desired ordering. Suppose we have two arrays like these: const arr1 = [1, 3, 2, 4, 5, 6]; const arr2 = [1, 2, 5]; console.log("Original arr1:", arr1); console.log("Reference arr2:", arr2); Original arr1: [ 1, 3, 2, 4, 5, 6 ] Reference arr2: [ 1, 2, 5 ] We want to sort arr1 so that elements appearing in ...
Read MoreConverting ASCII to hexadecimal in JavaScript
Converting ASCII characters to hexadecimal is a common task in JavaScript. Each character has an ASCII code that can be represented as a hexadecimal value. Understanding ASCII to Hex Conversion Each character in a string has an ASCII code. For example, '1' has ASCII code 49, '5' has 53, and '9' has 57. Converting to hexadecimal: 49 → 31, 53 → 35, 57 → 39. Example Here's how to convert ASCII characters to their hexadecimal representation: const str = '159'; const convertToHexa = (str = '') => { const res ...
Read MoreHow to filter out common array in array of arrays in JavaScript
When working with arrays of arrays in JavaScript, you may need to remove duplicate subarrays and keep only unique ones. This is useful for data deduplication and filtering operations. The Problem Suppose we have an array of arrays with duplicate subarrays: const arr = [ [ "Serta", "Black Friday" ], [ "Serta", ...
Read MoreCalculate the hypotenuse of a right triangle in JavaScript
We are required to write a JavaScript function that takes in two numbers. The first number represents the length of the base of a right triangle and the second is perpendicular. The function should then compute the length of the hypotenuse based on these values. For example − If base = 8, perpendicular = 6 Then the output should be 10 The Pythagorean Theorem The hypotenuse is calculated using the Pythagorean theorem: c² = a² + b², where c is the hypotenuse and a, b are the other two sides. ...
Read MoreCombine two different arrays in JavaScript
Suppose we have two arrays, the first array contains the scheduled date for some events and the second array contains the names of those events, like this − const dates = [ { id: "1", date: "2017-11-07" }, { id: "1", date: "2017-11-08" }, { ...
Read MoreCenter of each side of a polygon in JavaScript
In JavaScript, finding the center (midpoint) of each side of a polygon involves calculating the average coordinates between consecutive vertices. This is useful in computational geometry for polygon analysis and graphics applications. Problem Definition Given an array of coordinate pairs representing polygon vertices, we need to find the midpoint of each side. Each side connects two consecutive vertices, and the last vertex connects back to the first vertex to close the polygon. // Example polygon coordinates (longitude, latitude) const polygonVertices = [ [-73.9280684530257, 40.8099975343718], [-73.9282820374729, 40.8100875554645], ...
Read MoreJavaScript function that generates all possible combinations of a string
We are required to write a JavaScript function that takes in a string as the only argument. The function should generate an array of strings that contains all possible combinations of characters from the string. Understanding the Problem The function uses a bitwise approach to generate all possible combinations. It first extracts individual characters, then uses binary representation to determine which characters to include in each combination. Example Following is the code − const str = 'Delhi'; const allCombinations = (str1 = '') => { const arr = []; ...
Read MoreIterate over an object and remove false property in JavaScript
In JavaScript, you may need to recursively iterate through an object and remove properties with falsy values. This is common when cleaning up configuration objects or filtering data structures. The Problem Consider an object with nested structures containing "enabled" properties. We want to remove any objects where enabled is false, and also clean up any empty parent objects left behind. const obj = { a: { someKey: { propOne: '', ...
Read MoreGrouping array values in JavaScript
Suppose we have an array of objects containing some years and weeks data like this: const arr = [ {year: 2017, week: 45}, {year: 2017, week: 46}, {year: 2017, week: 47}, {year: 2017, week: 48}, {year: 2017, week: 50}, {year: 2017, week: 52}, {year: 2018, week: 1}, {year: 2018, week: 2}, {year: 2018, week: 5} ]; We are required to ...
Read MoreReturn Vowels in a string in JavaScript
We are required to write a JavaScript function that takes in a string that might contain some alphabets. The function should count and return the number of vowels that exists in the string. Syntax function countVowels(str) { // Convert to lowercase for case-insensitive comparison // Loop through each character // Check if character is a vowel (a, e, i, o, u) // Return the count } Example: Using for Loop Following is the code − const ...
Read More