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 136 of 589
Digital root sort algorithm JavaScript
Digital root of a positive integer is defined as the sum of all of its digits. We are given an array of integers and need to sort it based on digit root values. If a number comes before b, then the digit root of a should be less than or equal to the digit root of b. If two numbers have the same digit root, the smaller number should come first. For example, 4 and 13 have the same digit root (4 and 1+3=4), but since 4 < 13, the number 4 comes before 13 in the sorted array. ...
Read MoreHow to display only the current year in JavaScript?
To display only the current year in JavaScript, use the getFullYear() method with the Date object. This method returns a four-digit year as a number. Syntax new Date().getFullYear() Example: Display Current Year in HTML Current Year Display The Current Year: document.getElementById("currentYear").innerHTML = new Date().getFullYear(); ...
Read MoreReturning an array with the minimum and maximum elements JavaScript
Finding the minimum and maximum elements in a JavaScript array is a common task. This tutorial shows how to return both values in a single array using different approaches. const arr = [12, 54, 6, 23, 87, 4, 545, 7, 65, 18, 87, 8, 76]; We need to write a function that finds the minimum and maximum elements and returns them as an array with minimum at index 0 and maximum at index 1. Using Array.reduce() Method The reduce() method provides an efficient way to traverse the array once and track both minimum and ...
Read MoreDemonstrate nested loops with return statements in JavaScript?
In JavaScript, you can use return statements inside nested loops to exit from functions early. When a return statement is executed inside nested loops, it immediately exits the entire function, not just the current loop. Example let demoForLoop = () => { for(var outer = 1; outer < 100; outer++){ for(var inner = 1; inner { for(let i = 1; i
Read MoreFill missing numeric values in a JavaScript array
We are given an array of n entries, of which only 2 are Numbers, all other entries are null. Something like this: const arr = [null, null, -1, null, null, null, -3, null, null, null]; We are supposed to write a function that takes in this array and complete the arithmetic series of which these two numbers are a part. To understand this problem more clearly, we can think of these null values as blank space where we need to fill numbers so that the whole array forms an arithmetic progression. About Arithmetic Progression ...
Read MoreSum from array values with similar key in JavaScript
When working with arrays of data, you often need to group and sum values based on a common key. This article demonstrates how to sum array values with similar keys using JavaScript's reduce() method. Let's say we have an array containing stock transaction data for a company over time: const transactions = [ ['AAPL', 'buy', 100], ['WMT', 'sell', 75], ['MCD', 'buy', 125], ['GOOG', 'sell', 10], ['AAPL', 'buy', 100], ['AAPL', 'sell', 100], ...
Read MoreHow to splice duplicate item in array JavaScript
We have an array of Number / String literals that contain some duplicate values, we have to remove these values from the array without creating a new array or storing the duplicate values anywhere else. We will use the Array.prototype.splice() method to remove entries in-place, and we will take help of Array.prototype.indexOf() and Array.prototype.lastIndexOf() method to determine the duplicacy of any element. The Problem with Forward Iteration When using forEach() to remove duplicates, we encounter index shifting issues. Here's why the basic approach doesn't work perfectly: const arr = [1, 4, 6, 1, 2, 5, ...
Read MoreHow to combine 2 arrays into 1 object in JavaScript
Let's say, we have two arrays of equal lengths and are required to write a function that maps the two arrays into an object. The corresponding elements of the first array becomes the corresponding keys of the object and the elements of the second array become the value. We will reduce the first array, at the same time accessing elements of the second array by index. This is a common pattern for creating objects from parallel arrays. Using Array.reduce() Method const keys = [ 'firstName', 'lastName', 'isEmployed', ...
Read MoreHow to Sort object of objects by its key value JavaScript
Let's say, we have an object with keys as string literals and their values as objects as well like this − const companies = { 'landwaves ltd': {employees: 1200, worth: '1.2m', CEO: 'Rajiv Bansal'}, 'colin & co': {employees: 200, worth: '0.2m', CEO: 'Sukesh Maheshwari'}, 'motilal biscuits': {employees: 975, worth: '1m', CEO: 'Rahul Gupta'}, 'numbtree': {employees: 1500, worth: '1.5m', CEO: 'Jay Kumar'}, 'solace pvt ltd': {employees: 1800, worth: '1.65m', CEO: 'Arvind Sangal'}, 'ambicure': {employees: 170, worth: '0.1m', CEO: 'Preetam Chawla'}, ...
Read MoreOrder an array of words based on another array of words JavaScript
When working with arrays in JavaScript, you may need to reorder an array of objects based on the order specified in another array. This is useful for sorting data according to custom priorities or sequences. Let's say we have an array of objects sorted by their id property: const unordered = [{ id: 1, string: 'sometimes' }, { id: 2, string: 'be' }, { id: 3, string: 'can' }, { ...
Read More