Using one array to help filter the other in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

169 Views

When working with arrays of objects, you often need to filter one array based on values from another array. This is a common requirement when you have a list of items and want to keep only those that match specific criteria. Problem Statement Given an array of objects and an array of values, we need to filter the objects array to include only those objects whose property matches values in the second array. Sample Data Let's start with these arrays: const main = [ {name: "Karan", age: 34}, {name: "Aayush", ... Read More

Can the string be segmented in JavaScript

Sakshi Jain
Updated on 15-Mar-2026 23:19:00

454 Views

The string segmentation problem determines if a given string can be broken down into words that exist in a provided dictionary. This is a classic dynamic programming problem with practical applications in natural language processing and text analysis. What is String Segmentation? String segmentation involves breaking a continuous string into meaningful words using a dictionary. For example, given the string "haveapplepie" and dictionary ["have", "apple", "pie"], we can segment it as "have" + "apple" + "pie". // Example visualization const dictionary = ["apple", "have", "pie"]; const input = "haveapplepie"; // Can be segmented as: "have" + ... Read More

Alternative sorting of an array in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

670 Views

We need to write a JavaScript function that sorts an array in an alternating pattern where elements follow the sequence: smaller, larger, smaller, larger, and so on. The pattern we want to achieve is: arr[0] < arr[1] > arr[2] < arr[3] > arr[4] < arr[5]... This creates a zigzag or wave-like pattern in the sorted array. There can be multiple valid solutions for any given input array. Example Input and Output For an input array: const arr = [1, 2, 3, 4, 5, 6]; One possible output could be: ... Read More

Finding the longest non-negative sum sequence using JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

262 Views

Problem We are required to write a JavaScript function that takes in an array containing a sequence of integers, each element of which contains a possible value ranging between -1 and 1. Our function should return the size of the longest sub-section of that sequence with a sum of zero or higher. Understanding the Algorithm The solution uses a prefix sum approach with an index mapping technique. It tracks cumulative sums and uses an array to store the first occurrence of each sum value, allowing efficient calculation of subarray lengths. Example Following is the ... Read More

How to create a Circle with border colour using FabricJS?

Rahul Gurung
Updated on 15-Mar-2026 23:19:00

393 Views

In this tutorial, we are going to create a Circle with border colour using FabricJS. Circle is one of the various shapes provided by FabricJS. In order to create a circle, we will create an instance of fabric.Circle class and add it to the canvas. Since FabricJS is extremely flexible, we are allowed to customize our circle object in any way we like. One of the properties that FabricJS provides is borderColor which allows us to manipulate the colour of the border when our object is active. Syntax new fabric.Circle({ borderColor: String }: Object) Parameters ... Read More

How to find the sum of all elements of a given array in JavaScript?

Imran Alam
Updated on 15-Mar-2026 23:19:00

1K+ Views

In JavaScript, finding the sum of all elements in an array is a common operation. This tutorial covers different methods to achieve this, from traditional loops to modern array methods. Using the for Loop The for loop is the most straightforward approach to sum array elements. It provides full control over the iteration process and is easy to understand. Example Sum Array Elements Finding sum using for loop: ... Read More

How to crop the width in a cloned image using FabricJS?

Rahul Gurung
Updated on 15-Mar-2026 23:19:00

508 Views

In this tutorial, we are going to learn how to crop the width in a cloned image 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 crop the width in a cloned image, we use the width property with the cloneAsImage method. Syntax cloneAsImage(callback: function, options: Object): fabric.Object Parameters callback (optional) − This parameter is a function which is ... Read More

Generate PDF in ElectronJS

Prerna Tiwari
Updated on 15-Mar-2026 23:19:00

2K+ Views

Electron is a popular framework for building cross-platform desktop applications using JavaScript, HTML, and CSS. One common requirement in desktop applications is the ability to generate PDF documents programmatically. In this article, we will explore how to generate PDF files in Electron using the jsPDF library. We will cover the basics of PDF generation, installation steps, and various customization options to create professional-looking documents. PDF (Portable Document Format) is a file format developed by Adobe Systems that preserves document formatting across different platforms and devices. PDF files can contain text, images, tables, and other elements while maintaining consistent ... Read More

Differences in two strings in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

536 Views

We are required to write a JavaScript function that takes in two strings and find the number of corresponding dissimilarities in the strings. The corresponding elements will be dissimilar if they are not equal at the same position. Example Strings Let's say the following are our strings: const str1 = 'Hello world!!!'; const str2 = 'Hellp world111'; In this case, we need to compare each character at the same index and count how many positions have different characters. Method 1: Using a For Loop const str1 = 'Hello world!!!'; const str2 ... Read More

Removing property from a JSON object in JavaScript

Nikhilesh Aleti
Updated on 15-Mar-2026 23:19:00

7K+ Views

A JSON (JavaScript Object Notation) object is a data structure surrounded by curly braces {}. JSON objects contain key-value pairs where keys must be strings and values can be numbers, strings, objects, booleans, arrays, or null. JavaScript provides several methods to remove properties from objects. Each method has different characteristics - some modify the original object, while others create new objects without the unwanted properties. Basic JSON Object Structure Here's the basic syntax for a JSON object: const jsonObject = { "name": "John", "age": 30, "city": ... Read More

Advertisements