Articles on Trending Technologies

Technical articles with clear explanations and examples

Rounding off numbers to some nearest power in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 227 Views

We are required to write a JavaScript function that takes in a number and returns a number that can be represented as a power of 2 which is nearest to the input number. For example: If the input number is 145, the output should be 128 because 128 (which is 2^7) is the nearest power of 2 to 145. Understanding Powers of 2 Powers of 2 are numbers like: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512... Each number is double the previous one. Algorithm Approach The algorithm works by: ...

Read More

Merging two arrays in a unique way in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 186 Views

We are required to write a JavaScript function that takes in two arrays and merges the arrays taking elements alternatively from the arrays. For example If the two arrays are − const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3]; Then the output should be − [4, 2, 3, 1, 2, 6, 5, 8, 6, 9, 8, 4, 9, 3] Using Index-Based Approach The code for this will be − const arr1 = [4, 3, 2, ...

Read More

How to count the occurrence of a specific string in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 287 Views

In JavaScript, counting occurrences of a substring within a string is a common task. There are several approaches to accomplish this, from simple loops to built-in string methods. For example, counting how many times "is" appears in "this is a string" should return 2. count('this is a string', 'is') should return 2; Method 1: Using indexOf() with Loop The most reliable approach uses indexOf() to find each occurrence and increment a counter: const str1 = 'this is a string'; const str2 = 'is'; function countOccurrences(mainStr, subStr) { ...

Read More

Finding longest substring between two same characters JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 594 Views

In this problem, we need to find the longest substring between two same characters in a string using JavaScript. We'll use a Map to track character positions and calculate distances between matching characters. Problem Explanation Given a string like 'abcdefa', we have two 'a' characters with 'bcdef' between them (5 characters). This becomes our longest substring between same characters. Algorithm Steps Step 1 − Define function longestSubstring with parameter 'str'. Step 2 − Initialize maxLength = 0 and begin = -1 to track the longest substring. Step 3 − Create a Map called charMap to store ...

Read More

Pair of similar elements at different indices in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 147 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument. The function is required to count the number of all such element pairs from the array that are equal in magnitude but are present at different indices. For example, if the input array is: const arr = [7, 9, 5, 7, 7, 5]; Then the output should be: 4 because the desired pairs are [7, 7], [7, 7], [7, 7], [5, 5] How It Works The ...

Read More

Checking for univalued Binary Search Tree in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 197 Views

A binary search tree is univalued if every node in the tree has the same value. This means all nodes must contain identical data values for the tree to be considered univalued. Problem We need to write a JavaScript function that takes the root of a BST and returns true if the tree is univalued, false otherwise. For example, if the nodes of the tree are: const input = [5, 5, 5, 3, 5, 6]; Then the output should be: const output = false; Binary Search Tree Implementation ...

Read More

Sorting one string by the order of second in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 282 Views

We are required to write a JavaScript function that takes in two strings, str1 and str2 as the first and the second argument. Our function should sort str1 according to the order of characters as they appear in str2. Characters that appear first in str2 are placed first, followed by ones that come later, and finally followed by letters absent in str2. Problem Example For example, if the input to the function is: Input const str1 = 'coding'; const str2 = 'gncabdi'; Expected Output gncdio Output Explanation Looking at str2 ...

Read More

How to hide the controlling borders of an Ellipse using FabricJS?

Rahul Gurung
Rahul Gurung
Updated on 15-Mar-2026 294 Views

In this tutorial, we are going to learn how to hide the controlling borders of an 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. We can customize our controlling borders in many ways such as adding a specific color to it, a dash pattern etc. However, we can also eliminate the borders completely by using the hasBorders property. Syntax new fabric.Ellipse({ hasBorders: Boolean }: Object) Parameters ...

Read More

How to flip a Textbox horizontally using FabricJS?

Rahul Gurung
Rahul Gurung
Updated on 15-Mar-2026 346 Views

In this tutorial, we are going to learn how we can flip a Textbox object horizontally using FabricJS. We can customize, stretch or move around the text written in a textbox. In order to create a textbox, we will have to create an instance of fabric.Textbox class and add it to the canvas. We can flip a textbox object horizontally using the flipX property. Syntax new fabric.Textbox(text: String, { flipX: Boolean }: Object) Parameters text − This parameter accepts a String which is ...

Read More

Adding up identical elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 156 Views

We are required to write a JavaScript function that takes in an array of numbers and sums all the identical numbers together at one index. Problem Overview When we have an array with duplicate values, we want to combine them into a single occurrence with their sum. The first occurrence of each unique number will contain the total sum of all occurrences. Example Input and Output If the input array is: const arr = [20, 10, 15, 20, 15, 10]; Then the output should be: const output = [40, 20, ...

Read More
Showing 14691–14700 of 61,297 articles
Advertisements