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
Web Development Articles
Page 272 of 801
Converting array into increasing sequence in JavaScript
An increasing sequence in JavaScript is an array where each element is less than or equal to the next element. This article shows how to determine if an array can be converted to an increasing sequence by modifying at most one element. Problem Definition We define an array as increasing if arr[i] { const isIncreasing = (array) => { for (let i = 1; i < array.length; i++) { if (array[i] < array[i ...
Read MoreCan split array into consecutive subsequences in JavaScript
We are required to write a JavaScript function that takes in an array of sorted integers and determines if we can split it into consecutive subsequences of at least 3 elements each. Problem Statement Our function should return true if we can split the array into 1 or more subsequences where each subsequence consists of consecutive integers and has length at least 3, false otherwise. Input: const arr = [1, 2, 3, 3, 4, 5]; Expected Output: true Explanation: We can split the array into two consecutive subsequences: 1, 2, ...
Read MoreFinding two closest elements to a specific number in an array using JavaScript
Finding the two closest elements to a specific number in an array is a common algorithmic problem. This article demonstrates how to solve it efficiently using JavaScript's array methods and mathematical calculations. Problem We are required to write a JavaScript function that takes in an array of sorted integers, arr, as the first argument and a target number, as the second argument. Our function should return an array of exactly two numbers that exists in the array arr and are closest to target. The output array should also be sorted in increasing order. For example, if ...
Read MoreFinding longest consecutive joins in JavaScript
We are required to write a JavaScript function that takes in an array of pairs of numbers, arr, as the first and the only argument. In every pair, the first number is always smaller than the second number. Now, we define a pair (c, d) that can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion. Our function is supposed to find the length longest chain which can be formed. Problem Example For example, if the input to the function is: const arr ...
Read MoreMaximum average of a specific length of subarray in JavaScript
Problem We are required to write a JavaScript function that takes in an array of integers, arr, as the first argument and a number, num, as the second argument. Our function should find the contiguous subarray of given length num that has the maximum average value. And we need to output the maximum average value. Example Input and Output For example, if the input to the function is: const arr = [1, 12, -5, -6, 50, 3]; const num = 4; The expected output is: 12.75 Output Explanation: ...
Read Moreagent.createConnection() Method in Node.js
The agent.createConnection() method is an interface provided by the Node.js http module. This method produces a socket/stream that can be used for HTTP requests. You can override this method in custom agents for greater flexibility. A socket/stream can be returned either directly from this function or by passing it to the callback. Syntax agent.createConnection(options, [callback]) Parameters The above function accepts the following parameters: options – These options contain the connection details for which the stream has to be created. ...
Read MoreCreating an Agent in Node.js
In Node.js, an HTTP Agent manages connection pooling for HTTP client requests. You can create a custom agent using the new Agent() constructor to control connection behavior like keep-alive settings and socket limits. Syntax new http.Agent({options}) Parameters The Agent constructor accepts an options object with the following configurable properties: keepAlive – Keeps sockets open for reuse instead of closing them after each request. Default: false ...
Read MoreLogging in Node.js
Logging is a very essential part in any application whether it is made in Node.js or any other programming languages. Logging helps us to detect weird behaviours of an application along with real-time errors and exceptions. One should definitely put logical logs in their application. These logs help the user to identify any mistakes and resolve it on urgent basis. There are 5 different log levels which are present at the moment with the user. These log levels are used to define different kinds of logs and helps the user to identify different scenarios. The log levels must be ...
Read Morecrypto.createDiffieHellman() Method in Node.js
The crypto.createDiffieHellman() method in Node.js creates a Diffie-Hellman key exchange object using a specified prime value and an optional generator. This method enables secure key exchange between two parties over an insecure channel. Syntax crypto.createDiffieHellman(prime, [primeEncoding], [generator], [generatorEncoding]) Parameters prime – The prime number used for the Diffie-Hellman exchange. Can be a number (bit length) or Buffer/string containing the prime value. primeEncoding ...
Read Morecrypto.createHash() Method in Node.js
The crypto.createHash() method creates a hash object that can generate hash digests using cryptographic algorithms like SHA-256, MD5, or SHA-512. It's commonly used for password hashing, data integrity verification, and digital signatures. Syntax crypto.createHash(algorithm, [options]) Parameters algorithm – The hashing algorithm to use (string). Common values: 'sha256', 'md5', 'sha512', 'sha1' options – Optional parameters for controlling stream behavior and output length for certain algorithms Return Value Returns a Hash object that can be used to generate hash digests by chaining update() and digest() methods. Example 1: Basic Hash Generation ...
Read More