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
Iterating through an array, adding occurrences of a true in JavaScript
Suppose we have an array of true/false values represented by 't'/'f' which we retrieved from some database like this − const arr = ['f', 't', 'f', 't', 't', 't', 'f', 'f', 't', 't', 't', 't', 't', 't', 'f', 't']; console.log(arr); [ 'f', 't', 'f', 't', 't', 't', 'f', 'f', 't', 't', 't', 't', 't', 't', 'f', 't' ] We need to count consecutive occurrences of 't' that are sandwiched between two 'f's and return an array of those counts. Array: ['f', 't', 'f', 't', 't', 't', 'f', 'f', 't', ...
Read MoreDifference between product and sum of digits of a number in JavaScript
We are required to write a JavaScript function that takes in a positive integer as the only argument. The function should first calculate the sum of the digits of the number and then their product. Finally, the function should return the absolute difference between the product and the sum. For example, if the input number is 12345: Sum of digits: 1 + 2 + 3 + 4 + 5 = 15 Product of digits: 1 × 2 × 3 × 4 × 5 = 120 Absolute difference: |120 - 15| = 105 Example ...
Read MoreIndex difference of tuples in JavaScript
Problem We are required to write a JavaScript function that takes in an array of integers, arr, as the first and the only argument. Suppose two indices, i and j in the array which satisfy the following conditions − i < j, and arr[i] { let max = 0; const stack = [0]; ...
Read MoreASCII to hex and hex to ASCII converter class in JavaScript
In this tutorial, we'll create a JavaScript class that converts between ASCII and hexadecimal formats. This is useful for encoding text data, debugging, or working with binary protocols. Problem Statement We need to write a JavaScript class with two member functions: toHex: Takes an ASCII string and returns its hexadecimal equivalent toASCII: Takes a hexadecimal string and returns its ASCII equivalent For example, the string 'this is a string' should convert to hex '74686973206973206120737472696e67' and back to the original ASCII. How It Works ASCII to hex ...
Read MoreHow to hide the controlling corners of an Ellipse using FabricJS?
In this tutorial, we are going to learn how to hide the controlling corners of an Ellipse using FabricJS. Ellipse is one of the various shapes provided by FabricJS. In order to create an ellipse, we have to create an instance of fabric.Ellipse class and add it to the canvas. The controlling corners of an object allow us to increase or decrease its scale, stretch or change its position. We can customize our controlling corners in many ways such as adding a specific color to it, changing its size etc. However, we can also hide them using the hasControls property. ...
Read MoreHow to flip a Textbox vertically using FabricJS?
In this tutorial, we are going to learn how we can flip a Textbox object vertically 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. We can flip a textbox object vertically using the flipY property. Syntax new fabric.Textbox(text: String, { flipY: Boolean }: Object) Parameters text − This parameter accepts a String which is ...
Read MoreFinding place value of a number in JavaScript
In JavaScript, finding the place value of digits means extracting each digit's positional worth. For example, in the number 1234, the place values are [1000, 200, 30, 4]. We need to write a function that takes a positive integer and returns an array with the place values of all digits. Problem Example If the input number is: const num = 1234; Then the output should be: [1000, 200, 30, 4] Using Recursive Approach This problem is well-suited for recursion since we need to process each digit and calculate ...
Read MoreGenerating Random Prime Number in JavaScript
We are required to write a JavaScript function that takes in two numbers specifying a range. Our function should return a random prime number that falls in that range. Algorithm Overview The solution uses the Sieve of Eratosthenes algorithm to find all prime numbers in the range, then randomly selects one. This approach ensures efficiency for finding multiple primes. Example The code for this will be − const range = [100, 1000]; const getPrimes = (min, max) => { const result = Array(max + 1) ...
Read MoreSpecial arrays in JavaScript
In JavaScript, special arrays refer to arrays with unique characteristics or behaviors that differ from regular arrays. The main types include typed arrays for binary data, sparse arrays with missing elements, and arrays with non-numeric properties. Typed Arrays Typed arrays are array-like objects that provide a mechanism for accessing raw binary data. They have a fixed length and store elements of a specific numeric type, making them ideal for working with binary data efficiently. Common Typed Array Types // 8-bit signed integers const int8 = new Int8Array(4); int8[0] = 127; // max value ...
Read MoreCounting substrings of a string that contains only one distinct letter in JavaScript
We are required to write a JavaScript function that takes in a string as the only argument. The task of our function is to count all the contiguous substrings in the input string that contains exactly one distinct letter. The function should then return the count of all such substrings. For example, if the input string is 'iiiji', then the output should be 8 because the desired substrings are: 'i', 'i', 'i', 'ii', 'ii', 'iii', 'j', and 'i'. How It Works The algorithm identifies consecutive groups of identical characters and counts all possible contiguous substrings within ...
Read More