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
Get intersection between two ranges in JavaScript
In JavaScript, finding the intersection between two numerical ranges involves determining the overlapping portion. A range is typically represented as an array with two elements: [start, end]. Understanding Range Intersection The intersection of two ranges is the overlapping segment. For ranges [2, 5] and [4, 7], the intersection is [4, 5] because that's where they overlap. const arr1 = [2, 5]; const arr2 = [4, 7]; console.log("Range 1:", arr1); console.log("Range 2:", arr2); Range 1: [ 2, 5 ] Range 2: [ 4, 7 ] Algorithm To find the intersection, we ...
Read MoreComparing corresponding values of two arrays in JavaScript
When working with arrays in JavaScript, you might need to compare corresponding elements to determine which array has more "dominant" values at each position. This article demonstrates how to build a function that compares two arrays element by element and returns a result indicating which array has more greater values. Problem Statement Given two arrays of equal length, we need to compare corresponding elements and return: -1 if the first array has more elements greater than their corresponding elements in the second array 1 if the second array has more elements greater than their corresponding elements ...
Read MoreInterchanging first letters of words in a string in JavaScript
We need to write a JavaScript function that takes a string containing exactly two words and swaps their first letters to create a new string. Problem Statement Given a string with two words separated by a space, we want to interchange the first character of each word while keeping the rest of the characters in their original positions. Example For input "hello world", we should get "wello horld" where 'h' and 'w' are swapped. const str = 'hello world'; const interchangeChars = (str = '') => { const [first, second] ...
Read MoreHow to create a String representation of an Image object using FabricJS?
In this tutorial, we are going to show how you can create a String representation of an Image object using FabricJS. We can create an Image object by creating an instance of fabric.Image. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. In order to create a String representation of an Image object, we use the toString method. Syntax toString(): String Using the toString Method Let's see a code example to see the logged output when the toString method is ...
Read MoreES2022 JavaScript at() Method
JavaScript ES2022 introduced the at() method for strings, providing a more robust way to retrieve characters at specific positions. Unlike the traditional charAt() method, at() supports negative indexing and handles edge cases more gracefully. Syntax string.at(index) Where string is the target string and index is the position (zero-based). Returns the character at the specified index, or undefined if the index is out of range. Basic Usage Here's how to retrieve the first character of a string: ...
Read MoreHow to change the value of a global variable inside of a function using JavaScript?
In JavaScript, variables can be declared with either global or local scope. Understanding how to modify global variables from within functions is essential for managing application state. Global Variables are declared outside any function and can be accessed from anywhere in the code. Local Variables are declared inside functions and are only accessible within that specific function scope. There are two main approaches to change global variable values inside functions: Direct assignment Using window object with bracket notation Method 1: Direct Assignment The most straightforward way is to directly assign a new value ...
Read MoreHow to get the most common values in array: JavaScript ?
We are required to write a JavaScript function that takes in an array of literals that have repeating values. Our function should return an array of the most common element(s) in the array (if two or more elements appear for the same number of most times then the array should contain all those elements). Example The code for this will be − const arr1 = ["a", "c", "a", "b", "d", "e", "f"]; const arr2 = ["a", "c", "a", "c", "d", "e", "f"]; const getMostCommon = arr => { const count = ...
Read MorePositive, negative and zeroes contribution of an array in JavaScript
In JavaScript, you may need to analyze an array of integers to find the fractional ratio of positive, negative, and zero values. This is useful for statistical analysis and data processing tasks. Problem Statement Given an array of integers containing positive, negative, and zero values: const arr = [23, -1, 0, 11, 18]; We need to write a function that calculates the fractional ratio for each group: negative, zero, and positive numbers. The output should be an array of three decimal values representing these ratios. For the above array with length 5, the ...
Read MoreNumber difference after interchanging their first digits in JavaScript
We are required to write a JavaScript function that takes in an array of exactly two numbers. Our function should return the absolute difference between the numbers after interchanging their first digits. For instance, for the array [105, 413]: Original numbers: 105 and 413 After interchanging first digits: 405 and 113 The difference will be: |405 - 113| = 292 Algorithm The approach involves converting numbers to strings, extracting first digits, creating new numbers with swapped first digits, and calculating the absolute difference. Example const arr = [105, 413]; const ...
Read MoreHow to make a polygon object zoom-in and zoom-out using FabricJS?
We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized by any closed shape consisting of a set of connected straight line segments. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. To implement zoom functionality, we use the mouse:wheel event listener which detects mouse wheel movements and adjusts the canvas zoom level accordingly. Syntax canvas.on("mouse:wheel", callbackFunction); Example 1: Applying Zoom Controls on a Polygon Object Let's see a code example ...
Read More