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 131 of 589
How to get only the first BOT ID from thes JavaScript array?
When working with an array of objects in JavaScript, you often need to access specific properties from the first element. Let's explore how to get the first BOT ID from an array of user records. Sample Data Structure Consider an array of objects where each object contains a BOTID and Name: let objectArray = [ { BOTID: "56", Name: "John" }, { BOTID: "57", Name: "David" }, { BOTID: "58", Name: "Sam"}, { BOTID: "59", Name: "Mike" }, ...
Read MoreFinding out the Harshad number JavaScript
Harshad numbers are those numbers which are exactly divisible by the sum of their digits. Like the number 126, it is completely divisible by 1+2+6 = 9. All single digit numbers are harshad numbers. Harshad numbers often exist in consecutive clusters like [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [110, 111, 112], [1010, 1011, 1012]. Our job is to write a function that takes in ...
Read MoreTransform nested array into normal array with JavaScript?
In JavaScript, nested arrays can be flattened into a single-level array using several methods. The most common approach is the flat() method, which removes one level of nesting by default. What is Array Flattening? Array flattening transforms a nested array structure into a single-dimensional array. For example, converting [[1, 2], [3, 4]] into [1, 2, 3, 4]. Using flat() Method The flat() method creates a new array with sub-array elements flattened into it. Here's how to flatten a nested array containing objects: const arrayObject = [ [ ...
Read MoreCreate a polyfill to replace nth occurrence of a string JavaScript
A polyfill is a piece of code that provides functionality that isn't natively supported. In this tutorial, we'll create a polyfill to remove the nth occurrence of a substring from a string in JavaScript. Problem Statement We need to create a polyfill function removeStr() that extends the String prototype. The function should: subStr → the substring whose nth occurrence needs to be removed num → the occurrence number to remove (1st, 2nd, 3rd, etc.) Return the modified string if successful, or -1 if the nth occurrence doesn't exist ...
Read MoreCreate empty array of a given size in JavaScript
In JavaScript, you can create an empty array of a given size using several approaches. The most common method is using the Array() constructor. Using Array Constructor The new Array(size) creates an array with the specified length, but all elements are undefined (empty slots). var numberArray = new Array(5); console.log("Array length:", numberArray.length); console.log("Array contents:", numberArray); console.log("First element:", numberArray[0]); Array length: 5 Array contents: [ ] First element: undefined Filling the Array with Values After creating an empty array, you can assign values to specific positions or replace the entire ...
Read MoreValidate input: replace all 'a' with '@' and 'i' with '!'JavaScript
We need to write a function validate() that takes a string as input and returns a new string where all occurrences of 'a' are replaced with '@' and all occurrences of 'i' are replaced with '!'. This is a classic string manipulation problem that can be solved using different approaches. Let's explore the most common methods. Using For Loop The traditional approach iterates through each character and builds a new string: const string = 'Hello, is it raining in Amsterdam?'; const validate = (str) => { let validatedString = ''; ...
Read MoreHow to generate array of n equidistant points along a line segment of length x with JavaScript?
To generate an array of n equidistant points along a line segment of length x, we divide the segment into equal intervals and calculate each point's position based on its proportional distance. Syntax for (let i = 0; i < n; i++) { let ratio = (i + 1) / (n + 1); let point = ratio * segmentLength; // Add point to array } Example function generateEquidistantPoints(n, segmentLength) { const points = []; ...
Read MoreHow to sum elements at the same index in array of arrays into a single array? JavaScript
We have an array of arrays and are required to write a function that takes in this array and returns a new array that represents the sum of corresponding elements of original array. If the original array is: [ [43, 2, 21], [1, 2, 4, 54], [5, 84, 2], [11, 5, 3, 1] ] Then the output should be: [60, 93, 30, 55] Let's explore different approaches to solve this problem. Using forEach() Method The most straightforward approach is to iterate through each sub-array and accumulate ...
Read MoreFunction to create diamond shape given a value in JavaScript?
In JavaScript, you can create a function to generate diamond-shaped patterns using stars and spaces. A diamond shape consists of two parts: an upper triangle that expands and a lower triangle that contracts. Example function createDiamondShape(size) { // Upper part of diamond (including middle) for (var i = 1; i = i; s--) { process.stdout.write(" "); } // Print stars for (var j = 1; j
Read MoreGet the property of the difference between two objects in JavaScript
When working with JavaScript objects, you often need to compare them and identify which properties have different values. This is useful for tracking changes, validation, or debugging purposes. Problem Overview Given two objects with similar key-value pairs, we need to write a function that finds the first key with different values between the objects. If all values match, the function should return -1. Here are sample objects to demonstrate the concept: const obj1 = { name: 'Rahul Sharma', id: '12342fe4554ggf', isEmployed: true, ...
Read More