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
How to filter values from an array using the comparator function in JavaScript?
In JavaScript, filtering an array can be done by using the filter() method. This method creates a new array with all elements that pass the test implemented by the provided callback function (comparator function). Syntax arr.filter(callback) Here callback is a comparator function used to test each element in the array arr. It should return true to keep the element, or false to filter it out. Using Array.filter() Method The most efficient way to filter values is using the built-in filter() method. Here's an example filtering odd numbers: ...
Read MoreHow to change the font family of Text using FabricJS?
In this tutorial, we are going to learn about how to change the font family of Text object using FabricJS. We can display text on canvas by adding an instance of fabric.Text. Not only does it allow us to move, scale and change the dimensions of the text but it also provides additional functionality like text alignment, text decoration, line height which can be obtained by the properties textAlign, underline and lineHeight respectively. We can change the font family by using the fontFamily property. Syntax new fabric.Text(text: String, { fontFamily: String }: Object) Parameters ...
Read MoreGrouping of same kind of numbers in JavaScript
We have an array of numbers with both negative and non-negative values. Our goal is to count consecutive groups of non-negative numbers (positives and zeros) in the array. const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]; console.log("Array:", arr); Array: [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0] In this array, we need to identify consecutive groups of non-negative numbers. Looking at the array: Index 3: single element 0 forms one group Indices 9-11: elements 0, 1, 0 form another group ...
Read MoreFiltering array of objects in JavaScript
Filtering arrays of objects is a common task in JavaScript. This article shows how to filter an array of objects based on values from another array using the filter() and includes() methods. Problem Statement Suppose we have two arrays - one containing literal values and another containing objects: const source = [1, 2, 3, 4, 5]; const cities = [{ city: 4 }, { city: 6 }, { city: 8 }]; console.log("Source array:", source); console.log("Cities array:", cities); Source array: [1, 2, 3, 4, 5] Cities array: [{ city: 4 }, { city: ...
Read MoreGroup matching element in array in JavaScript
In JavaScript, grouping matching elements in an array is a common task that can be efficiently solved using the reduce() method. This approach allows us to iterate through an array and group consecutive duplicate elements together. What is the reduce() Function? The reduce() method is a built-in JavaScript function that processes each element of an array and accumulates a result. It takes a callback function with two main parameters: an accumulator (which stores the accumulated result) and the current value being processed. Basic reduce() Example const numbers = [1, 2, 3, 4, 5]; const sum ...
Read MoreNon-negative set subtraction in JavaScript
In JavaScript, non-negative set subtraction involves removing elements from one set that exist in another set, while ensuring the result contains only non-negative values. This operation combines set theory with value filtering using JavaScript's built-in Set object and forEach method. What is the Set Object? The Set is a built-in data structure introduced in ES6 that stores unique values of any type. Unlike arrays, Sets automatically eliminate duplicates and maintain insertion order. Key characteristics include: Uniqueness: Only unique elements are stored ...
Read MoreCalculating time taken to type words in JavaScript
In JavaScript, we can calculate the time taken to type words on a custom keyboard where keys are arranged alphabetically (a-z) instead of the traditional QWERTY layout. Problem Setup We make two key assumptions: The fingertip starts at index 0 (key 'a') Time to move between keys equals the absolute difference of their positions. For example, moving from 'a' (index 0) to 'k' (index 10) takes |0 - 10| = 10 time units Example Walkthrough For the string 'dab', the movements are: 'a' ...
Read MoreCurrified function that multiples array elements in JavaScript
Problem We need to write a JavaScript function that takes an array and returns another function. This returned function takes a number and produces a new array where each element is the product of the corresponding element from the original array and the number. What is a Curried Function? A curried function breaks down a function that takes multiple arguments into a series of functions that each take a single argument. In this case, instead of multiply(array, number), we have multiply(array)(number). Example Following is the code − const arr = [2, 5, 2, 7, 8, 4]; ...
Read MoreFinding the first non-consecutive number in an array in JavaScript
In JavaScript, finding the first non-consecutive number in an array means identifying the first element that is not exactly one more than its previous element. This is useful for detecting gaps in sequential data. Problem Statement We need to write a JavaScript function that takes an array of numbers and returns the first element that breaks the consecutive sequence. The function should return the element that is not the natural successor (+1) of its previous element. Algorithm The approach is to iterate through the array and compare each element with its previous element. When we find ...
Read MoreForming palindrome using at most one deletion in JavaScript
We need to write a JavaScript function that checks if a string can be made into a palindrome by deleting at most one character. Problem Given a string, determine if it can become a palindrome after removing at most one character. A palindrome reads the same forwards and backwards. For example, with the input string 'dr.awkward', we can remove the '.' character to get 'drawkward', which is a palindrome. Algorithm Approach We use a two-pointer technique: Compare characters from both ends moving inward When we find a mismatch, try removing either the left or ...
Read More