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
The Ackermann Function is a classic example of a recursive function that grows extremely quickly. It's notable for being a total computable function that is not primitive recursive, making it an important concept in theoretical computer science. Problem Statement We need to write a JavaScript function that takes two non-negative integers, m and n, and returns the Ackermann number A(m, n) defined by the following mathematical definition: A(m, n) = n+1 if m=0 A(m, n) = A(m-1, 1) if m>0 and n=0 A(m, n) = A(m-1, A(m, n-1)) if m>0 and n>0 Implementation ... Read More
In this tutorial, we are going to learn how to lock the flipping during scaling of an Ellipse using FabricJS. Just as we can specify the position, color, opacity and dimension of an ellipse object in the canvas, we can also specify whether we want to stop flipping an object during scaling. This can be done by using the lockScalingFlip property. Syntax new fabric.Ellipse({ lockScalingFlip : Boolean }: Object) Parameters options (optional) − This parameter is an Object which provides additional customizations to our ellipse. Using this parameter color, cursor, ... Read More
When working with JavaScript objects containing arrays of nested objects, you often need to find which parent key contains an object with specific property values. This is common when dealing with product catalogs, user groups, or categorized data. Example Data Structure Consider this product catalog object where each category contains an array of products: const obj = { "LAPTOP": [{ "productId": "123" }], "DESKTOP": [{ "productId": "456" ... Read More
In this problem statement, our task is to find all the possible odd length subarrays with the help of JavaScript functionalities. This task can be done with the help of some built-in functions of JavaScript or we can solve it by multiple for loops. Logic for the Given Problem The problem states that we have to get all the possible odd length subarrays in JavaScript programming language. The meaning of odd length is that the length of the subarrays should be 1, 3, 5, 7, and so on. So our task is to filter the subarrays with odd ... Read More
Suppose we have an array of integers that is already sorted in the increasing order. We are required to write a JavaScript function that without using the inbuilt Array.prototype.sort() method sorts the array like the following − First number should be the maximum Second number should be the minimum Third number should be the 2nd ... Read More
Validating push/pop sequences is a common stack problem where we need to verify if a given pop sequence could result from a specific push sequence on an initially empty stack. Problem Statement Given two arrays pushed and popped containing unique elements, determine if the pop sequence could be achieved from the push sequence using stack operations. const pushed = [1, 2, 3, 4, 5]; const popped = [4, 5, 3, 2, 1]; How It Works The algorithm simulates stack operations by maintaining a stack and two pointers. We push elements until the stack ... Read More
A Boggle board is a 2D array of individual characters. We need to validate whether a given word can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without reusing any previously used cells. Problem Given a Boggle board like this: const board = [ ["I", "L", "A", "W"], ["B", "N", "G", "E"], ["I", "U", "A", "O"], ["A", "S", "R", "L"] ]; We need to check if words like "BINGO" or "LINGO" are valid. Valid words are ... Read More
Adding binary numbers without converting to decimal requires simulating manual binary addition with carry handling. This approach processes bits from right to left, just like traditional addition. Problem Statement We need to write a JavaScript function that takes two binary strings and returns their sum as a binary string, without converting to decimal numbers. Input: const str1 = '1101'; const str2 = '10111'; Expected Output: '100100' Algorithm Overview The solution works by: Reversing both strings to process from least significant bit Adding corresponding bits with carry propagation Building the ... Read More
In this tutorial, we are going to learn how to lock the horizontal movement of an Ellipse using FabricJS. Just as we can specify the position, color, opacity and dimension of an ellipse object in the canvas, we can also specify whether we want it to move only in the Y-axis. This can be done by using the lockMovementX property. Syntax new fabric.Ellipse({ lockMovementX: Boolean }: Object) Parameters options (optional) − This parameter is an Object which provides additional customizations to our ellipse. ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance