Articles on Trending Technologies

Technical articles with clear explanations and examples

Removing listener from inside outer function in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 210 Views

When working with event listeners in JavaScript, you might need to remove a listener from within an outer function. This is accomplished using removeEventListener() by passing the element reference and function reference to the outer function. How It Works The key is to pass both the element (this) and the function reference to the outer function, then use removeEventListener() to detach the listener. Example Remove Event Listener Example Press Me ...

Read More

JavaScript: compare array element properties, and if identical, combine

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 187 Views

When working with arrays of objects, you often need to combine objects that have identical properties. In this example, we'll consolidate storage devices with the same size by adding up their counts. Suppose we have an array of objects containing information about data storage devices: const drives = [ {size:"900GB", count:3}, {size:"900GB", count:100}, {size:"1200GB", count:5}, {size:"900GB", count:1} ]; console.log("Original array:", drives); Original array: [ { size: '900GB', count: 3 }, { size: '900GB', count: ...

Read More

Maximum difference between a number JavaScript

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

In this problem statement, our task is to write a function for finding the maximum difference between two numbers in an array using JavaScript. We'll explore both a brute force approach and an optimized solution. Understanding the Problem We need to find the maximum difference between any two numbers in an array where the larger number appears after the smaller number in the array order. For example, in array [2, 3, 5, 6, 4, 1], the maximum difference is 4 (6 - 2 = 4), not 5 (6 - 1), because 1 comes after 6. Method 1: ...

Read More

Check whether we can form string2 by deleting some characters from string1 without reordering the characters of any string - JavaScript

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

In this problem, we need to determine whether we can form string2 by removing some characters from string1 without changing the order of characters in either string. This is essentially checking if string2 is a subsequence of string1. Understanding the Problem We want to check if the second string can be formed from the first string while preserving character order. Let's look at some examples: // Example 1: Can form "ale" from "apple" let s1 = "apple"; let s2 = "ale"; console.log("Can form '" + s2 + "' from '" + s1 + "'?"); // Expected: ...

Read More

Forming and matching strings of an array based on a random string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 394 Views

In JavaScript, we can check if strings from an array can be formed using characters from a given string. This involves counting character frequencies and matching them against each array element. Problem Statement Given an array of strings and a random string, find the first string from the array that can be completely formed using the characters available in the random string. const arr = ['Dinesh', 'Mahesh', 'Rohit', 'Kamal', 'Jatin Sapru', 'Jai']; const str = 'lsoaakjm'; console.log("Array:", arr); console.log("Available characters:", str); Array: [ 'Dinesh', 'Mahesh', 'Rohit', 'Kamal', 'Jatin Sapru', 'Jai' ] Available ...

Read More

Bring number down to 1 in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 196 Views

Problem We need to write a JavaScript function that takes a number and finds the minimum operations to reduce it to 1 using these rules: If the number is even, divide it by 2 If the number is odd, add 1 or subtract 1 The function should return the minimum number of operations required. Example For input number 7, the output should be 4 operations: Input: 7 Output: 4 Output Explanation The optimal path requires 4 steps: 7 → 8 → 4 → 2 → 1 ...

Read More

Finding the sum of all numbers in the nth row of an increasing triangle using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 371 Views

For the purpose of this problem, an increasing triangle is a structure where numbers are arranged in rows, with each row containing one more number than the previous row, and all numbers are consecutive starting from 1. Understanding the Triangle Structure The increasing triangle looks like this: 1 2 3 4 5 6 7 8 9 10 Each row n contains n consecutive numbers. Row 1 has 1 number, row 2 has 2 numbers, row 3 has 3 numbers, and so on. Problem Statement We need ...

Read More

How to set the padding of an Ellipse using FabricJS?

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

In this tutorial, we are going to learn how to set the padding 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. Just as we can specify the position, color, opacity and dimension of an ellipse object in the canvas, we can also set the padding of an ellipse object. This can be done by using the padding property. Syntax new fabric.Ellipse({ padding : Number }: Object) ...

Read More

How to set the padding of Textbox using FabricJS?

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

In this tutorial, we are going to learn how to set the padding of a Textbox using FabricJS. Textbox is one of the various shapes provided by FabricJS. In order to create a textbox, we will have to create an instance of fabric.Textbox class and add it to the canvas. Just as we can specify the position, colour, opacity and dimension of a textbox object in the canvas, we can also set the padding of a textbox object. This can be done by using the padding property. Syntax new fabric.Textbox(text: String, { padding : Number }: Object) ...

Read More

FabricJS – How to scale an Image object to a given height?

Rahul Gurung
Rahul Gurung
Updated on 15-Mar-2026 1K+ Views

In this tutorial, we are going to learn how to scale an Image object to a given height 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 scale an Image object to a given height, we use the scaleToHeight method. Syntax scaleToHeight(value: Number, absolute: Boolean): fabric.Object Parameters value − This parameter accepts a Number which determines ...

Read More
Showing 15121–15130 of 61,299 articles
Advertisements