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 144 of 589
What should be the correct Algorithm to Get Array B from Array A counting backwards from the last element in JavaScript?
Consider the following binary array (Array A) − const arr = [1, 0, 1, 1, 1, 1, 0, 1, 1]; When this array is passed through the function, say sumRight(), it produces the following output array (Array B) − const output = [1, 0, 4, 3, 2, 1, 0, 2, 1]; Understanding the Algorithm Elements in array arr can be either 0 or 1. The function counts backward from the last element of array arr, if there are consecutive 1's in the array arr then the corresponding element in the output ...
Read MoreJavaScript Return the lowest index at which a value should be inserted into an array once it has been sorted (either in ascending or descending order).
We have to write a function that returns the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted (either in ascending or descending order). The returned value should be a number. For example, Let's say, we have a function getIndexToInsert() − getIndexToInsert([1, 2, 3, 4], 1.5, 'asc') should return 1 because it is greater than 1 (index 0), but less than 2 (index 1). Likewise, getIndexToInsert([20, 3, 5], 19, 'asc') should return 2 because once the array has been sorted in ...
Read MoreIs having the first JavaScript parameter with default value possible?
In JavaScript, you can have default values for the first parameter, but you need special techniques to skip it when calling the function. The most effective approach is using destructuring with spread syntax. The Challenge When you define a function with a default value for the first parameter, you cannot simply omit it in a regular function call: function multiply(firstParam = 10, secondParam) { return firstParam * secondParam; } // This won't work as expected: multiply(5); // firstParam = 5, secondParam = undefined Solution: Using Destructuring with Spread Syntax ...
Read MoreHow to remove every Nth element from an array JavaScript?
Removing every Nth element from an array is a common task in JavaScript. This can be accomplished using the Array.prototype.splice() method to modify the array in-place, or by creating a new array with the filtered elements. Method 1: Using splice() (In-Place Modification) The splice() method removes elements from the array and modifies the original array: const arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']; console.log("Original array:", arr); const removeNth = (arr, n) => { // Start from n-1 index and remove every nth element ...
Read MoreIterating and printing a JSON with no initial key and multiple entries?
When working with JSON arrays containing multiple objects, you can iterate through each entry using JavaScript's forEach() method. This is particularly useful when your JSON data doesn't have a single root key and contains multiple entries. Syntax array.forEach((element, index) => { // Process each element }); Example: Iterating Through Student Records const details = [ { "studentId": 101, "studentName": "John Doe" }, ...
Read MoreExtract properties from an object in JavaScript
We have to write a JavaScript function, say extract() that extracts properties from an object to another object and then deletes them from the original object. For example − If obj1 and obj2 are two objects, then obj1 = {color:"red", age:"23", name:"cindy"} obj2 = extract(obj1, ["color", "name"]) After passing through the extract function, they should become like − obj1 = { age:23 } obj2 = {color:"red", name:"cindy"} Therefore, let's write the code for this function − Syntax const extract = (obj, ...keys) => { ...
Read MoreConvert an array of binary numbers to corresponding integer in JavaScript
Let's say, we have an array of Numbers that contains 0 and 1 − const arr = [0, 1, 0, 1]; We are required to write an Array function, toBinary() that returns the corresponding decimal integer for the array it is used with. For example − If the array is − const arr = [1, 0, 1, 1]; Then the output should be 11 because the decimal representation of binary 1011 is 11. Therefore, let's write the code for this function. Method 1: Using parseInt() with join() In ...
Read MoreHow to delete all the DB2 packages in collection COLL1?
A DB2 collection is a physical quantity which is used to group the packages. A collection can be simply termed as a group of DB2 packages. By using collections we can bind the same DBRM into different packages. In order to delete all the DB2 packages under a collection, we can issue the FREE PACKAGE command. Syntax FREE PACKAGE(collection_name.*) Example: Deleting All Packages in COLL1 To delete all packages in the collection named COLL1, use the following command: // DB2 command to delete all packages in COLL1 collection console.log("Executing: FREE PACKAGE(COLL1.*)"); ...
Read MoreAggregate records in JavaScript
Aggregating records in JavaScript involves combining multiple objects with the same identifier into a single object with accumulated values. This is commonly used for data analysis tasks like summing transactions by person or grouping sales by product. Let's say we have an array of objects that contains information about some random transactions carried out by some people: const transactions = [{ name: 'Rakesh', amount: 1500 }, { name: 'Rajesh', amount: 1200 }, { name: 'Ramesh', ...
Read MoreHow can I declare and define multiple variables in one statement with JavaScript?
In JavaScript, you can declare and define multiple variables in a single statement by separating them with commas. This approach works with var, let, and const keywords. Syntax var variable1 = value1, variable2 = value2, variable3 = value3; // Or with let/const let variable1 = value1, variable2 = value2, variable3 = value3; Example: Using var var firstName = "My First Name is David", lastName = "My Last Name is Miller", ...
Read More