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
Removing the second number of the pair that add up to a target in JavaScript
Problem We need to write a JavaScript function that takes an array of numbers and a target sum. The function should remove the second number from any consecutive pair of numbers that add up to the target. Example Let's see how this works with a practical example: const arr = [1, 2, 3, 4, 5]; const target = 3; const removeSecond = (arr = [], target = 1) => { const res = [arr[0]]; for(let i = 1; i < arr.length; i++){ ...
Read MoreHow to create a cloned image object using FabricJS?
In this tutorial, we are going to show how you can create a cloned image object 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 create a cloned image object, we use the cloneAsImage method. Syntax cloneAsImage(callback: function, options: Object): fabric.Object Parameters callback (optional) − This parameter is a function which is invoked with a cloned image instance as ...
Read MoreHow to Change the Time Interval of setinterval() Method at RunTime using JavaScript ?
The setInterval() method is used to call a particular block of code repeatedly after a specific time interval. It accepts two parameters: the function to execute and the time interval in milliseconds. Sometimes you need to change this interval dynamically during runtime for irregular or variable timing patterns. JavaScript provides two main approaches to change the time interval of setInterval() at runtime: Using the clearInterval() method Using the setTimeout() method Let us understand both approaches with practical examples. Using the clearInterval() Method The clearInterval() method stops a ...
Read MoreHow to create a third object from two objects using the key values in JavaScript?
In JavaScript, you can create a third object from two objects by mapping key values between them. This is useful when you have one object containing arrays of keys and another object with corresponding values. Problem Statement Given two objects where the first contains arrays of keys and the second contains key-value pairs, we need to calculate totals for each category: const obj1 = { positive: ['happy', 'excited', 'joyful'], negative: ['depressed', 'sad', 'unhappy'] }; const obj2 = { happy: 6, excited: 1, unhappy: 3 }; ...
Read MoreReturn the greatest possible product of n numbers from the array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n, as the second argument. Our function should calculate and return the greatest possible product of n numbers from the array. Problem Analysis To find the maximum product of n numbers, we need to consider both positive and negative numbers. Two negative numbers multiplied together give a positive result, so the strategy involves: Sorting the array to identify the largest and smallest values Choosing ...
Read MoreConverting 12 hour Format Time to 24 hour Format in JavaScript
Converting 12 hour format time to 24 hour format in JavaScript can be easily achieved using various approaches which we will be discussing in this article. For conversion from 12 hour to 24 hour format we check for the modifier (AM or PM). Depending on the modifier we convert the time formats. In this article, we have specified a time at the beginning in 12 hour format. Our task is to convert 12 hour format time to 24 hour format in JavaScript. Approaches to Convert 12-hour Format to 24-hour Here is a list of approaches to convert ...
Read MoreFinding the immediate bigger number formed with the same digits in JavaScript
We need to write a JavaScript function that takes a number and rearranges its digits to form the smallest number that is just bigger than the input number using the same digits. For instance, if the input number is 112, the output should be 121. If no bigger number can be formed (like 321), we return -1. Algorithm Approach The brute force approach involves: Find the maximum possible number by sorting digits in descending order Check each number from input+1 to maximum Return the first number that uses the same digits Example Implementation ...
Read MoreHow to create a JSON representation of an Image object using FabricJS?
In this tutorial, we are going to learn how to create a JSON representation of an Image object 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 create a JSON representation of an Image object, we use the toJSON method. Syntax toJSON(propertiesToInclude: Array): Object Parameters propertiesToInclude − This parameter accepts an Array which contains any properties we might want ...
Read MoreSum of distinct elements of an array in JavaScript
Suppose, we have an array of numbers like this − const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1]; We are required to write a JavaScript function that takes in one such array and counts the sum of all distinct elements of the array. For example: The output for the array mentioned above will be − 30 Method 1: Using lastIndexOf() and indexOf() This approach checks if the current index is the last occurrence of each element, ensuring we count each distinct element ...
Read MoreAbsolute difference of Number arrays in JavaScript
In JavaScript, calculating the absolute difference between corresponding elements of two arrays is a common operation. This involves subtracting elements at the same index positions and taking the absolute value of the result. Suppose we have two arrays like these: const arr1 = [1, 2, 3, 4, 5, 6]; const arr2 = [9, 8, 7, 5, 8, 3]; We need to create a function that returns an array of absolute differences between corresponding elements. For these arrays, the expected output should be: [8, 6, 4, 1, 3, 3] Using For Loop ...
Read More