Found 6683 Articles for Javascript

Changing the npm start-script of Node.js

Mayank Agarwal
Updated on 20-May-2021 11:21:38

10K+ Views

The start-script of a Node.js application consists of all the commands that will be used to perform the specific tasks. When starting or initializing a Node.js project, there are a lot of predefined scripts that will be created for running the application. These scripts can be changed as per the need or demand of the project.Script commands are widely used for making different startup scripts of programs in both Node and React. 'npm start' is used for executing a startup script without typing its execution command.Package.json FileThis is the start-up script that needs to be added in the package.json file. ... Read More

agent.createConnection() Method in Node.js

Mayank Agarwal
Updated on 20-May-2021 11:20:24

548 Views

The agent.createConnection() method is an interface provided by the 'http' module. This method produces a socket/stream that can be used for the HTTP requests. One can use custom agents to override this method for greater flexibility. A socket/stream can be returned in two ways – either by returning the socket/stream directly from this function, or by passing this socket/stream to the callback.Syntaxagent.createConnection(options, [callback])ParametersThe above function can accept the following parameters −options – These options will contain the connection details for which stream has to be created.callback – This will receive the created socket connection from the agent.ExampleCreate a file with the name ... Read More

Implementing insertion sort to sort array of numbers in increasing order using JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 09:54:28

947 Views

The art of sorting arrays is of paramount importance in the realm of programming, as it allows for efficient organization and manipulation of data. When it comes to implementing a reliable sorting algorithm, insertion sort emerges as a versatile and effective option. In this article, we delve into the intricate world of JavaScript to explore the process of implementing insertion sort to arrange an array of numbers in increasing order. By comprehending the underlying mechanics of this algorithm and harnessing the power of JavaScript's capabilities, developers can unlock the potential to efficiently sort and organize numerical data, enhancing the performance ... Read More

Regrouping characters of a string in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:35:22

69 Views

ProblemWe are required to write a JavaScript function that takes in a string str as the first and the only argument.The string str can contain three types of characters −English alphabets: (A-Z), (a-z)Numerals: 0-9Special Characters − All other remaining charactersOur function should iterate through this string and construct an array that consists of exactly three elements, the first contains all alphabets present in the string, second contains the numerals and third the special characters maintain the relative order of characters. We should finally return this array.For example, if the input to the function isInputconst str = 'thi!1s is S@me23';Outputconst output ... Read More

Maximum average of a specific length of subarray in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:34:13

87 Views

ProblemWe 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.For example, if the input to the function isInputconst arr = [1, 12, -5, -6, 50, 3]; const num = 4;Outputconst output = 12.75;Output ExplanationBecause the desired subarray is [12, -5, -6, 50]ExampleFollowing is the code − Live Democonst arr = [1, 12, -5, -6, 50, 3]; const num ... Read More

Finding longest consecutive joins in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:33:48

89 Views

ProblemWe 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.For example, if the input to the function isInputconst arr = [    [1, 2], [2, 3], [3, 4] ];Outputconst ... Read More

Finding two closest elements to a specific number in an array using JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:33:16

560 Views

ProblemWe 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 the input to the function isInputconst arr = [1, 2, 3, 4, 5]; const target = 3;Outputconst output = [2, 3];ExampleFollowing is the code − Live Democonst arr = [1, 2, 3, 4, 5]; const target = 3; const findClosest ... Read More

Can split array into consecutive subsequences in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:30:38

187 Views

ProblemWe are required to write a JavaScript function that takes in an array of sorted integers, arr, as the first and the only argument.Our function should return true if and only if we can split the array into 1 or more subsequences such that each subsequence consists of consecutive integers and has length at least 3, false otherwise.For example, if the input to the function isInputconst arr = [1, 2, 3, 3, 4, 5];Outputconst output = true;Output ExplanationWe can split them into two consecutive subsequences −1, 2, 3 3, 4, 5ExampleFollowing is the code − Live Democonst arr = [1, 2, ... Read More

Converting array into increasing sequence in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:30:07

154 Views

Increasing SequenceWe define an array as increasing if arr[i]

Total number of longest increasing sequences in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:28:25

710 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument.Our function is required to find the number of longest increasing subsequences (contiguous or non-contiguous).For example, if the input to the function isInputconst arr = [2, 4, 6, 5, 8];Outputconst output = 2;Output ExplanationThe two longest increasing subsequences are [2, 4, 5, 8] and [2, 4, 6, 8].ExampleFollowing is the code − Live Democonst arr = [2, 4, 6, 5, 8]; const countSequence = (arr) => {    const distance = new Array(arr.length).fill(1).map(() => 1)    const count = ... Read More

Advertisements