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
Articles on Trending Technologies
Technical articles with clear explanations and examples
How to clone a given regular expression in JavaScript?
JavaScript's regular expression support is widely considered to be among the best in the world, but there's one area where it's lacking: there's no built-in way to clone a regular expression. This can be a problem when you need to create a new regular expression that is similar to an existing one but with a few small changes. The problem is that regular expressions are objects, and when you assign one regex to another variable, both variables reference the same object in memory. The Problem with Direct Assignment let regex1 = /foo/g; let regex2 = regex1; ...
Read MoreHow to stop the loop for JavaScript scroll down?
JavaScript scroll down loops can be extremely annoying and disruptive to a web page. Fortunately, it is relatively easy to stop the loop if you know what steps to take. In this article, we will discuss how to stop the JavaScript scroll down loop so that your website visitors don't have to keep scrolling endlessly. We'll also provide some tips on preventing these types of issues in the future. JavaScript method scrollBy() scrolls the web page to the specific number of pixels. When used in loops or intervals, it can create continuous scrolling effects that need proper control mechanisms. ...
Read MoreHow to add two arrays into a new array in JavaScript?
An ordered group of indexed elements is represented by an array, which is a type of data structure. Merging two or more arrays to create a larger array that contains all the items from the original arrays is a typical operation done on multiple arrays. In JavaScript, there are several ways to add two arrays together and create a new array. This article will go over how to properly use the concat() method and spread operator (...) to combine two separate arrays into one newly created array. Additionally, each of these methods will be discussed with examples so ...
Read MoreCalculating the LCM of multiple numbers in JavaScript
We are required to write a JavaScript function that takes in an array of numbers of any length and returns their LCM (Least Common Multiple). We will approach this problem in parts: Part 1 − We will create a helper function to calculate the Greatest Common Divisor (GCD) of two numbers Part 2 − Then using Part 1 helper function we will create another helper function to calculate the Least Common Multiple (LCM) of two numbers. Part 3 − Finally, using Part 2 helper function we will create a function that loops over the array and ...
Read MoreShift strings Circular left and right in JavaScript
The main objective for the problem statement is to perform a circular shift on the strings in Javascript. The circular shifts can be left shift or right shift. And implement this solution in Javascript. Understanding the Problem The problem at hand is to shift the strings circularly left and right with the help of Javascript functionalities. Circular shifting means we have to move the characters of the given string in a circular manner in which the characters that are to be shifted beyond the boundaries of the string should reappear at the opposite end. Logic for the ...
Read MoreCalculating average of a sliding window in JavaScript
We are required to write a JavaScript function that takes in an array of integers, arr, as the first argument and a number, num (num < length of arr) as the second argument. The function should construct and return a new array that contains the average of all possible num contiguous numbers of the array. For example − If the input array and the number are − const arr = [1, 2, 3, 4, 5]; const num = 2; Then the output should be − const output = [1.5, 2.5, 3.5, 4.5]; ...
Read MoreReplacing dots with dashes in a string using JavaScript
We are required to write a JavaScript function that takes in a string and replaces all appearances of dots(.) in it with dashes(-). Input const str = 'this.is.an.example.string'; Expected Output const output = 'this-is-an-example-string'; All appearances of dots(.) in string str are replaced with dash(-) Method 1: Using replace() with Regular Expression The most efficient approach is using the built-in replace() method with a global regular expression: const str = 'this.is.an.example.string'; const replaceDots = (str = '') => { return str.replace(/\./g, '-'); }; ...
Read MoreReturning the first number that equals its index in an array using JavaScript
Problem We need to write a JavaScript function that takes an array of numbers and returns the first number whose value equals its 0-based index position. This means we're looking for an element where array[i] === i. Example Following is the code − const arr = [9, 2, 1, 3, 6, 5]; const findFirstSimilar = (arr = []) => { for(let i = 0; i < arr.length; i++){ const el = arr[i]; if(el === i){ ...
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 MoreHow to create a canvas with progress cursor on hover over objects using FabricJS?
In this article, we are going to create a canvas with a progress cursor on hover using FabricJS. The progress cursor is one of the native cursor styles available which can be used in the FabricJS canvas too. FabricJS provides various types of cursors such as default, all-scroll, crosshair, col-resize, row-resize, etc. that reuse the native cursor under the hood. The hoverCursor property sets the style of the cursor when hovered over a canvas object. Syntax new fabric.Canvas(element: HTMLElement|String, { hoverCursor: String }: Object) Parameters ...
Read More