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 185 of 589
Calculating excluded average - JavaScript
In JavaScript, you may need to calculate the average of specific values in an array of objects based on certain conditions. This tutorial shows how to compute the average of values where a boolean flag indicates they should be included in the calculation. Problem Description Given an array of objects with val and canUse properties, we need to calculate the average of val values only for objects where canUse is true. const arr = [ {val: 56, canUse: true}, {val: 16, canUse: true}, {val: 45, canUse: true}, ...
Read MoreSplit keys and values into separate objects - JavaScript
In JavaScript, you often need to transform an object into an array of objects where each key-value pair becomes a separate object. This is useful for data processing, API responses, and working with libraries that expect array formats. Problem Statement Suppose we have an object like this: const dataset = { "diamonds": 77, "gold-bars": 28, "exciting-stuff": 52, "oil": 51, "sports-cars": 7, "bitcoins": 40 }; We need to write a JavaScript function that takes this object and returns an array of objects with ...
Read MoreSplitting string into groups – JavaScript
Given a string S, consisting of alphabets, numbers and special characters. We need to write a program to split the strings in three different strings S1, S2 and S3, such that − The string S1 will contain all the alphabets present in S, The string S2 will contain all the numbers present in S, and S3 will contain all special characters present in S. The strings S1, S2 and S3 should have characters in the same order as they appear in input. Syntax const ...
Read MoreChecking whether the sum of digits of a number forms a Palindrome Number or not in JavaScript
We are required to write a JavaScript function that takes in a number, sums its digits and checks whether that sum is a Palindrome number or not. The function should return true if the sum is Palindrome, false otherwise. For example, if the number is 697, then the sum of its digits will be 6 + 9 + 7 = 22, which indeed, is a Palindrome number. Therefore, our function should return true for 697. Understanding the Problem A palindrome number reads the same forwards and backwards. For example: 11, 22, 121, 1331 are palindromes. Our task ...
Read MoreFind the Symmetric difference between two arrays - JavaScript
In Mathematics, the symmetric difference of two sets, say A and B is represented by A △ B. It is defined as the set of all elements which belong either to A or to B but not to both. For example: const A = [1, 2, 3, 4, 5, 6, 7, 8]; const B = [1, 3, 5, 6, 7, 8, 9]; The symmetric difference of A and B will be: const diff = [2, 4, 9] Using For Loops with indexOf() This approach iterates through both arrays and checks ...
Read MoreHow to reverse a portion of an array in JavaScript?
We are required to write a JavaScript function that takes in an array, a start index and an end index. The function should reverse the portion of the array between the start index and end index. For example, if the array is: const arr = [2, 6, 5, 8, 3, 5, 2, 6, 7]; And the start index and end index are 3, 7 respectively, then the array should be reversed to: const output = [2, 6, 5, 2, 5, 3, 8, 6, 7]; Using Array Splice Method This approach ...
Read MoreSorting an associative array in ascending order - JavaScript
In JavaScript, you can sort an array of objects (associative array) in ascending order using the sort() method with a custom comparison function. Suppose we have an array of objects like this: const people = [ {"id":1, "name":"Andrew", "age":30, "gender":"m", "category":"G"}, {"id":2, "name":"Brandon", "age":25, "gender":"m", "category":"G"}, {"id":3, "name":"Christine", "age":20, "gender":"m", "category":"G"}, {"id":4, "name":"Elena", "age":29, "gender":"W", "category":"M"} ]; We need to sort this array by the age property in ascending order. The expected output should be: [ ...
Read MoreHow to get the numbers which can divide all values in an array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns a number which can exactly divide all the numbers in the array. Let's say the following is our array: const arr = [4, 6, 34, 76, 78, 44, 34, 26, 88, 76, 42]; Understanding the Problem We need to find the common divisors that can divide all numbers in the array. This is essentially finding the Greatest Common Divisor (GCD) of all array elements and then finding all its divisors. Method 1: Finding All Common Divisors ...
Read MoreHow can we make an Array of Objects from n properties of n arrays in JavaScript?
When working with multiple arrays in JavaScript, you often need to combine them into an array of objects. This is useful for creating structured data from separate arrays of related information. Suppose we have two arrays of literals like these: const options = ['A', 'B', 'C', 'D']; const values = [true, false, false, false]; We need to create a JavaScript function that combines these arrays into a new array of objects like this: [ {opt: 'A', val: true}, {opt: 'B', val: false}, {opt: 'C', val: false}, ...
Read MoreHow to insert an element into all positions in an array using recursion - JavaScript?
We are required to declare a function, let's say insertAllPositions, which takes two arguments — an element, x, and an array, arr. Functions must return an array of arrays, with each array corresponding to arr with x inserted in a possible position. That is, if arr is the length N, then the result is an array with N + 1 arrays — For example, the result of insertAllPositions(10, [1, 2, 3]) should be — const output = [ [10, 1, 2, 3], [1, 10, 2, 3], ...
Read More