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 67 of 589
Mapping an array to a new array with default values in JavaScript
In JavaScript, mapping an array to a new array with default values is a common task when you need to transform data while ensuring missing or invalid values are replaced with fallback values. What is Array Mapping? Array mapping creates a new array by transforming each element of an existing array through a callback function. The original array remains unchanged, and the new array has the same length. const numbers = [1, 2, 3]; const doubled = numbers.map(x => x * 2); console.log(numbers); // Original unchanged console.log(doubled); // New transformed array ...
Read MoreMaximum decreasing adjacent elements in JavaScript
In JavaScript, finding the maximum number of decreasing adjacent elements means identifying the longest consecutive sequence where each element is smaller than the previous one. This is essentially finding the longest decreasing subarray. Understanding the Problem We need to find the maximum count of consecutive decreasing pairs in an array. For example, in array [5, 4, 3, 2, 1], we have 4 decreasing pairs: (5, 4), (4, 3), (3, 2), and (2, 1). Finding Decreasing Adjacent Elements 5 arr[0] ...
Read MoreRemove json element - JavaScript?
In JavaScript, you can remove elements from JSON objects or arrays using different methods. This article demonstrates how to remove properties from JSON objects and elements from JSON arrays. Sample JSON Data Let's work with the following JSON array containing customer information: var details = [ { customerName: "Chris", customerAge: 32 }, { customerName: "David", ...
Read MoreJavaScript Group a JSON Object by Two Properties and Count
When working with JSON data, you often need to group objects by multiple properties and count occurrences. This is useful for data analysis, creating summaries, or generating reports from complex datasets. What is JSON? JSON (JavaScript Object Notation) is a lightweight data format for transferring data between systems. It uses key-value pairs where keys are strings and values can be various data types. Each entry is separated by commas, for example: {"name": "Peter", "age": 25}. const jsonData = [ { ...
Read MoreJavaScript Sum odd indexed and even indexed elements separately and return their absolute difference
For a given list of elements, write a JavaScript program to find the sum of its odd and even indexed elements and then, calculate difference between them. To solve this problem, we will first separate odd indexed and even indexed elements. After separating them find their sum separately and store it in different variables. Now, we will calculate the difference between these sums to get desired result. Example Scenario: Input: list = [11, 21, 31, 41, 51, 61]; Output: difference = 30 Here, odd-indexed items are [21, 41, 61] and their sum is ...
Read MoreJavaScript Count the number of unique elements in an array of objects by an object property
In JavaScript, when dealing with arrays of objects, it's common to need to extract specific values based on object properties and count how many unique occurrences exist for a particular property. Whether you're processing a list of users, products, or any other data structure, counting unique elements by a property is a useful operation. In this article, we'll discuss how to count the number of unique elements in an array of objects based on a specific property using a few different techniques. Problem Statement You have an array of objects representing users, each with properties like name, ...
Read MoreGenerate all combinations of supplied words in JavaScript
In JavaScript, there are scenarios where you may need to generate every possible combination of a string's characters. This can be especially useful in areas like cryptography, analyzing subsets of data, or solving problems involving permutations. In this article, we'll learn to implement a JavaScript function to achieve this task. Generate all combinations of supplied words Following are the different approaches for generating all combinations of supplied words − Recursive Approach Iterative Approach Using Bitmasking Using Recursive Approach The recursive approach builds combinations by including ...
Read MoreJavaScript - Find if string is a palindrome (Check for punctuation)
In JavaScript, checking if a string is a palindrome while handling punctuation requires cleaning the string first. A palindrome reads the same forwards and backwards, ignoring spaces, punctuation, and case. What is a Palindrome? A palindrome is not limited to single words like "radar" or "level"; it can include phrases or sequences like "Madam, in Eden, I'm Adam." We need to ignore punctuation, spaces, and case differences to ensure accurate palindrome checks. Using String and Array Methods The first approach uses JavaScript's built-in string and array methods to normalize and reverse the string: ...
Read MoreJavaScript - Finding the third maximum number in an array
In this article, we will learn to find the third maximum unique number in an array of JavaScript, or the largest number if there are fewer than three unique values. We will be using three methods: a single-pass approach for efficiency, a sorting method to identify the number after removing duplicates, and a combination of Set and Max-Heap for optimal handling of large datasets. Problem Statement Given an array of integers, find the third maximum unique number. If the array contains fewer than three unique numbers, return the largest number in the array. Input: const nums = ...
Read MoreJavaScript - Complementary Colors Builder
In this article, we will learn to build a JavaScript function that calculates the complementary color for a given hex color code. In web development, working with colors and their relationships is an essential aspect of design and user interface. One of the concepts that designers often use is complementary colors—colors that are opposite each other on the color wheel, providing contrast and enhancing visual appeal. You can try our Online Color Picker Tool to create new colors. Understanding Complementary Colors The following concepts help us understand complementary colors: Complementary Colors: ...
Read More