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
Maximum consecutive 1s after n swaps in JavaScript
We are required to write a JavaScript function that takes in a binary array (array that contains only 0 or 1) as the first argument, and a number as the second argument representing the maximum number of swaps allowed. We can change at most the specified number of 0s present in the array to 1s, and our function should return the length of the longest contiguous subarray that contains only 1s after making these changes. Problem Statement Given a binary array and a number of allowed swaps, find the maximum length of consecutive 1s we can achieve ...
Read MoreFinding the sum of floors covered by an elevator in JavaScript
Problem We need to write a JavaScript function that calculates the total number of floors covered by an elevator. The function takes an array representing the floor numbers where the elevator stopped, and returns the sum of distances traveled between consecutive stops. Understanding the Logic The elevator covers floors by moving between consecutive stops. If it goes from floor 7 to floor 1, it covers 6 floors (7-1). If it then goes from floor 1 to floor 7, it covers another 6 floors (7-1). The total distance is the sum of absolute differences between consecutive floors. ...
Read MoreRepresenting seconds in HH:MM:SS in JavaScript
Converting seconds to HH:MM:SS format is a common requirement in JavaScript applications for displaying durations, timers, and time intervals in a readable format. This article explores three practical methods to achieve this conversion. Problem Statement Given a number representing the duration in seconds, the task is to write a JavaScript function that converts the given duration into the format HH:MM:SS, where HH represents hours, MM represents minutes, and SS represents seconds. Sample Input: const durationInSeconds = 3665; Sample Output: 01:01:05 In this case, the input durationInSeconds is 3665, which represents a ...
Read MoreHow to create a canvas with an ellipse using FabricJS?
In this tutorial, we are going to learn how to create a canvas with an Ellipse object using FabricJS. Ellipse is one of the various shapes provided by FabricJS. In order to create an ellipse, we will create an instance of fabric.Ellipse class and add it to the canvas. Syntax new fabric.Ellipse({ rx: Number, ry: Number }: Object) Parameters options (optional) − This parameter is an Object which provides additional customizations to our ellipse. Using this parameter color, cursor, stroke width and a ...
Read MoreHow to change the font size of a Textbox using FabricJS?
In this tutorial, we are going to see how to change the font size of a Textbox 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. The font size specifies how large or small the characters displayed in our textbox should be. We can change the font size by using the fontSize property. Syntax new fabric.Textbox(text: String, { fontSize: Number }: Object) Parameters ...
Read MoreHow to validate an input is alphanumeric or not using JavaScript?
Validating whether an input contains only alphanumeric characters (letters and numbers, no spaces or special characters) is a common requirement in JavaScript applications. An alphanumeric string contains only numeric digits (0-9) and alphabetical characters (A-Z, a-z). Below are two effective approaches to validate alphanumeric strings in JavaScript. Using the charCodeAt() Method The charCodeAt() method returns the ASCII value of a character. We can iterate through each character and check if its ASCII value falls within the valid ranges: 48-57: Numeric characters (0-9) 65-90: Uppercase letters (A-Z) 97-122: Lowercase letters (a-z) Syntax ...
Read MoreGet values that are not present in another array in JavaScript
We are given two arrays: (arr1 and arr2) − arr1 contains some literal values. arr2 contains objects that map some literal values. We are required to write a JavaScript function that takes in two such arrays. Then the function should return an array of all the elements from arr1 that are not mapped by objects in arr2. Example The code for this will be − const arr1 = [111, 222, 333, 444]; const arr2 = [ { identifier: 111 }, ...
Read MoreList all duplicate values in a nested JavaScript object
When working with nested JavaScript objects, you may need to find duplicate values that appear at different levels of nesting. This tutorial shows how to recursively search through a nested object and identify all duplicate values. Problem Statement Consider a nested object containing pet information: const pets = { owner1: 'Frank', owner2: 'Curly', owner3: 'Maurice', dogs: { terriers: { name1: ...
Read MoreFind first duplicate item in array in linear time JavaScript
We are required to write a JavaScript function that takes in a read only array of n + 1 integers between 1 and n. The function should find one number that repeats in linear time and using at most O(n) space. For example If the input array is − const arr = [3, 4, 1, 4, 1]; Then the output should be − 4 If there are multiple possible answers (like above), we should output any one. If there is no duplicate, we should output -1. Using Set Data ...
Read MoreCommons including duplicates in array elements in JavaScript
We need to write a JavaScript function that finds all common characters across all strings in an array, including duplicates. If a character appears multiple times in every string, it should appear that many times in the result. Problem Statement Given an array of strings, return an array of characters that appear in all strings. The frequency of each character in the result should match the minimum frequency of that character across all strings. For example, with the input array ['door', 'floor', 'crook']: 'o' appears 2 times in "door", 2 times in "floor", and 2 times ...
Read More