Let's say we have a variable "users" that contains the following string of text where each user is separated by a semicolon and each attribute of each users is separated by a comma − const users = 'Bob, 1234, Bob@example.com;Mark, 5678, Mark@example.com'; We are required to write a JavaScript function that takes in one such string and splits this into a multidimensional array that looks like this − const arr = [ ['Bob', 1234, 'Bob@example.com'], ['Mark', 5678, 'Mark@example.com'] ]; Method 1: Using split() and for Loop ... Read More
Finding the pair of adjacent elements with the largest product is a common array problem in JavaScript. This involves iterating through the array and comparing products of consecutive elements. Problem Understanding Given an array of integers, we need to find two adjacent elements whose product is maximum among all possible adjacent pairs. For example, in array [1, 2, 3, 4], the adjacent pairs are (1, 2), (2, 3), and (3, 4) with products 2, 6, and 12 respectively. Method 1: Using -Infinity for Initialization This approach initializes the maximum product with -Infinity to handle arrays with ... Read More
The tribonacci sequence is a generalization of the Fibonacci sequence where each term is the sum of the three preceding terms. Unlike Fibonacci which uses two preceding terms, tribonacci uses three. The standard tribonacci series starts with 0, 1, 1, and each subsequent term is the sum of the previous three terms: 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149... How It Works Each term after the first three follows the formula: T(n) = T(n-1) + T(n-2) + T(n-3) Where T(0) = 0, T(1) = 1, T(2) = 1 ... Read More
In this tutorial, we are going to learn how to lock the vertical 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 X-axis. This can be done by using the lockMovementY property. Syntax new fabric.Ellipse({ lockMovementY: Boolean }: Object) Parameters options (optional) − This parameter is an Object which provides additional customizations to our ellipse. ... Read More
We are required to write a JavaScript function that takes in a number and returns a new number in which all the digits of the original number are squared and concatenated. For example: If the number is − 99 Then the output should be − 8181 because 9² is 81 and 9² is 81, so concatenating them gives 8181. Example Let's implement this function step by step: const num = 9119; const squared = num => { const numStr = String(num); ... Read More
We are required to write a JavaScript function that takes in an array of exactly two numbers specifying a range. The function should then calculate the least common multiple of all the numbers within that range and return the final result. Understanding LCM and GCD To find the LCM of multiple numbers, we first need helper functions for GCD (Greatest Common Divisor) and LCM of two numbers: GCD: The largest number that divides both numbers evenly LCM: The smallest positive number that is divisible by both numbers Formula: LCM(a, b) = (a × b) / ... Read More
In this problem statement, our objective is to check if a given input number is some power of another number using JavaScript functionalities. Logic for the Problem To determine if a number a is a power of another number b, we can use logarithms. If a = b^n for some integer n, then log_b(a) = n should be an integer. Since JavaScript's Math.log() returns the natural logarithm, we use the change of base formula: log_b(a) = ln(a) / ln(b). If this result is an integer, then a is a power of b. Algorithm Step 1 ... Read More
We are required to write a JavaScript function that takes in an array of integers as the first argument and an integer as the second argument. The function should check whether we can create n (second argument) subarrays from the original array such that all the subarrays have the equal sum. For example − If the inputs are − const arr = [4, 3, 2, 3, 5, 2, 1]; const num = 4; The output should be true because the subarrays are: [5], [1, 4], [2, 3], [2, 3] all having sum equal ... Read More
We need to write a JavaScript function that takes a string containing alphabets and whitespaces, then returns a new string with characters arranged in case-insensitive alphabetical order while removing whitespace and punctuation. Problem Statement Our function should iterate over the input string and concatenate characters into a new string following "case-insensitively-alphabetical-order-of-appearance" order. Whitespace and punctuation are simply removed. For example: Input: const str = 'some simple letter combination!'; Expected Output: abceeeeiiillmmmnnoooprssttt Solution Using Nested Loops The approach uses nested loops to iterate through each letter of the alphabet (a-z) ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance