In JavaScript, calculating the absolute difference between corresponding elements of two arrays is a common operation. This involves subtracting elements at the same index positions and taking the absolute value of the result. Suppose we have two arrays like these: const arr1 = [1, 2, 3, 4, 5, 6]; const arr2 = [9, 8, 7, 5, 8, 3]; We need to create a function that returns an array of absolute differences between corresponding elements. For these arrays, the expected output should be: [8, 6, 4, 1, 3, 3] Using For Loop ... Read More
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 More
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 More
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 More
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 More
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 More
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 More
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 More
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 More
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 More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance