Web Development Articles

Page 217 of 801

Can one string be repeated to form other in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 157 Views

We are required to write a JavaScript function that takes in two strings, str1 and str2, as the first and the second argument. Our function should return the minimum number of times we should repeat string str1 so that string str2 is a substring of it. If it is impossible for str2 to be a substring after repeating it, we should return -1. Problem Example For example, if the input to the function is: const str1 = 'wxyz'; const str2 = 'yzwxyzwx'; The expected output is 3, because by repeating str1 three times ...

Read More

Check if user inputted string is in the array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 533 Views

We need to write a JavaScript program that checks if a user-inputted string exists in a predefined array. The program should return true if the string is found, false otherwise. This is commonly needed when validating user input against a list of acceptable values, such as checking usernames, categories, or allowed options. Using Array.includes() Method The includes() method is the most straightforward way to check if an array contains a specific value. It returns a boolean result. Check ...

Read More

crypto.createDiffieHellman(primeLength, [generator]) Method in Node.js

Mayank Agarwal
Mayank Agarwal
Updated on 15-Mar-2026 146 Views

The crypto.createDiffieHellman(primeLength, [generator]) method creates a Diffie-Hellman key exchange object by generating a prime number of specified bit length. This is commonly used for secure key exchange between parties. Syntax crypto.createDiffieHellman(primeLength, [generator]) Parameters The parameters are described below: primeLength – The number of prime bits to generate. Must be a number. generator – Optional generator for creating the exchange key object. ...

Read More

Count by unique key in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 696 Views

When working with arrays of objects, you often need to count occurrences based on unique key values. This is particularly useful for data analysis, grouping records, or creating summary statistics. The Problem Given an array of objects with nested properties, we want to count how many times each unique user appears: const arr = [ { assigned_user: { name: 'Paul', ...

Read More

crypto.randomBytes() Method in Node.js

Mayank Agarwal
Mayank Agarwal
Updated on 15-Mar-2026 4K+ Views

The crypto.randomBytes() method generates cryptographically strong pseudo-random data in Node.js. This method ensures sufficient entropy before completion, typically taking only a few milliseconds to generate secure random bytes. Syntax crypto.randomBytes(size, [callback]) Parameters The parameters are described below: size – Specifies the number of bytes to generate. Must not exceed 2**31 - 1. callback – Optional callback function called if an error occurs. If omitted, the method runs synchronously. Asynchronous Example ...

Read More

Merge arrays in column wise to another array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 747 Views

When working with multiple arrays, you often need to combine their elements column-wise to create structured data. This technique merges arrays by their corresponding indices to form an array of objects. Problem Statement Suppose we have three arrays of numbers like these: const code = [123, 456, 789]; const year = [2013, 2014, 2015]; const period = [3, 4, 5]; We need to merge these arrays column-wise to create an array of objects where each object contains the corresponding elements from all three arrays: const output = [ ...

Read More

crypto.getCurves() Method in Node.js

Mayank Agarwal
Mayank Agarwal
Updated on 15-Mar-2026 372 Views

The crypto.getCurves() method returns an array containing names of all supported elliptic curves in Node.js. These curves are used for creating Elliptic Curve Diffie-Hellman (ECDH) key exchange objects for secure cryptographic operations. Syntax crypto.getCurves() Parameters This method takes no parameters since it returns a complete list of all available elliptic curves. Return Value Returns an array of strings, where each string represents the name of a supported elliptic curve. Example Here's how to use crypto.getCurves() to retrieve all available elliptic curves: // Importing the crypto module const crypto ...

Read More

Take in array of objects and convert the above JSON to a Tree-structure in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

In web development, you often need to convert flat arrays of hierarchical data into tree structures. This is common when working with organizational charts, file systems, or nested menus. Input Data Structure Suppose we have an array of objects representing parent-child relationships: const arr = [ { "parentIndex": '0', "childIndex": '3', "parent": "ROOT", "child": "root3" }, ...

Read More

process.argv() Method in Node.js

Mayank Agarwal
Mayank Agarwal
Updated on 15-Mar-2026 5K+ Views

The process.argv property returns an array containing command-line arguments passed when the Node.js process was launched. The first element is the path to the Node.js executable, and the second is the path to the JavaScript file being executed. Syntax process.argv Parameters process.argv is a property, not a method, so it doesn't take any parameters. It automatically captures all command-line arguments passed to the Node.js process. Basic Usage Example Create a file named argv.js and run it using the command node argv.js: // Node.js program to demonstrate the use of process.argv ...

Read More

How to process JavaScript nested array and display the order of numbers according to the level upto which they are nested?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 324 Views

Processing nested arrays and displaying elements with proper indentation based on their nesting level is a common JavaScript task. This technique helps visualize the hierarchical structure of nested data. Problem Statement Given a nested array like this: const arr = [23, 6, [2, [6, 2, 1, 2], 2], 5, 2]; We need to display numbers with indentation showing their nesting level. Elements at deeper levels should have more indentation. Expected Output 23 6 2 6 2 ...

Read More
Showing 2161–2170 of 8,010 articles
« Prev 1 215 216 217 218 219 801 Next »
Advertisements