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
Prime digits sum of a number in JavaScript
We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should then sum all the digits of the number that are prime and return the sum as a number. For example − If the input number is − const num = 67867852; Then the output should be − const output = 21; because 7 + 7 + 5 + 2 = 21 − Understanding Prime Digits Prime digits are single-digit prime numbers: 2, 3, 5, and ...
Read MoreChecking validity of equations in JavaScript
In JavaScript, we need to check if a set of variable equations can all be satisfied simultaneously. This involves parsing equality and inequality constraints and determining if consistent variable assignments exist. Problem Statement Given an array of string equations in the format 'X===Y' (equality) or 'X!==Y' (inequality), determine if we can assign values to variables such that all equations evaluate to true. Understanding the Logic The solution uses a Union-Find (Disjoint Set) data structure: Group equal variables into the same set Check if any inequality constraint violates the grouping If variables that must be ...
Read MoreCounting possible APs within an array in JavaScript
Arithmetic Progression (AP) is a sequence of numbers where the difference between any two consecutive numbers is constant (called the common difference). For instance, 1, 2, 3, 4, 5, 6, … is an AP with a common difference of 1 (2 - 1 = 1). Problem Statement We need to write a JavaScript function that takes a sorted array of integers and returns the count of arithmetic progressions of size 3 that can be formed from the array elements. In each progression, the differences between consecutive elements must be the same. The input array is guaranteed ...
Read MoreHow to disable the centered rotation of Ellipse using FabricJS?
In this tutorial, we are going to learn how to disable the centered rotation of Ellipse using FabricJS. Ellipse is one of the various shapes provided by FabricJS. In order to create an ellipse, we will have to create an instance of fabric.Ellipse class and add it to the canvas. By default, all objects in FabricJS use their center as the point of rotation. However, we can change this behaviour by using the centeredRotation property. Syntax new fabric.Ellipse({ centeredRotation: Boolean }: Object) Parameters ...
Read MoreHow to create a Textbox with text cursor on moving objects using FabricJS?
In this tutorial, we are going to create a Textbox with a text cursor on hover over objects using FabricJS. text is one of the native cursor styles available which can be used in the FabricJS canvas too. FabricJS provides various types of cursors like default, all-scroll, crosshair, col-resize, row-resize, etc., which are reusing the native cursor under the hood. The moveCursor property sets the style of the cursor when the object is moved around in the canvas. Syntax new fabric.Textbox(text: String, { moveCursor: String }: Object) Parameters ...
Read MoreHow to check whether the background image is loaded or not using JavaScript?
In web development, checking whether a background image has loaded is crucial for ensuring proper page display and user experience. JavaScript provides several methods to detect when images finish loading, allowing developers to take appropriate actions or display fallback content. When dealing with background images, we can use the Image object in JavaScript to monitor loading status. This approach works by creating an image element programmatically and listening for load events, even when the image is used as a CSS background. Methods to Check Image Loading Status Using the onload event handler ...
Read MoreKeeping only redundant words in a string in JavaScript
We are required to write a JavaScript function that takes in a string and returns a new string with only the words that appeared more than once in the original string. Problem Statement If the input string is: const str = 'this is a is this string that contains that some repeating words'; Then the output should be: 'this is that' The function should identify duplicate words and return them in the order they first appeared. Using indexOf() and lastIndexOf() This approach checks if a word's first occurrence ...
Read MoreInverting slashes in a string in JavaScript
When working with strings in JavaScript, you may need to replace forward slashes (/) with backslashes (\) or vice versa. This is common when dealing with file paths or URL manipulation. This article demonstrates how to create a function that takes a string containing forward slashes and converts them to backslashes. Problem Statement We need to write a JavaScript function that: Takes a string that may contain forward slashes (/) Returns a new string with all forward slashes replaced by backslashes (\) Method 1: Using a for Loop Here's a solution using a ...
Read MoreSorting array of strings having year and month in JavaScript
Suppose, we have an array of strings that contains month-year combined strings like this: const arr = ["2009-feb", "2009-jan", "2010-mar", "2010-jan", "2011-jul", "2011-sep", "2011-jan", "2012-jan", "2012-dec", "2012-feb", "2013-may", "2013-jul", "2013-jun", "2014-jan", "2014-dec", "2014-may", "2015-may", "2015-jan", "2015-jun", "2016-jan", "2016-dec"]; We need to write a JavaScript function that takes such an array and sorts these dates from oldest to latest order. Approach The solution involves creating a custom sorting function that: Splits each date string to separate year and month Converts month names to numeric values ...
Read MoreFinding missing element in an array of numbers in JavaScript
We are required to write a JavaScript function that takes in an array of numbers of length, say n. The array contains all the integers from 0 to n (including both 0 and n), but just one integer is missing, it can be any number and the array is not sorted. The task of our function is to find the missing number and return it in linear time and constant space. Since the array contains all the numbers from 0 to n but one, we can simply calculate the sum of all the elements of the array in linear ...
Read More