Special type of sort of array of numbers in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

183 Views

We are required to write a JavaScript function that takes in an array of numbers and sorts the array such that first all the even numbers appear in ascending order and then all the odd numbers appear in ascending order. For example: If the input array is − const arr = [2, 5, 2, 6, 7, 1, 8, 9]; Expected Output Then the output should be − [2, 2, 6, 8, 1, 5, 7, 9] Using Custom Comparator Function We can solve this by creating a custom comparator function ... Read More

Writing table of number in array in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

177 Views

We are required to write a JavaScript function that takes in two numbers, say m and n, and it returns an array of first n multiples of m. Therefore, let's write the code for this function − Example The code for this will be − const num1 = 4; const num2 = 6; const multiples = (num1, num2) => { const res = []; for(let i = num1; i { return Array.from({length: count}, (_, index) => base * (index + 1)); }; console.log(generateTable(7, 5)); console.log(generateTable(3, 8)); [ 7, 14, 21, 28, 35 ] [ 3, 6, 9, 12, 15, 18, 21, 24 ] Using a Simple For Loop Here's another straightforward approach using a basic for loop: function createMultiplicationTable(number, count) { const table = []; for (let i = 1; i

Random whole number between two integers JavaScript

Nikitasha Shrivastava
Updated on 15-Mar-2026 23:19:00

308 Views

In JavaScript, generating a random whole number between two integers is a common task that requires combining Math.random() with Math.floor() to ensure we get integers within the specified range. Understanding the Problem We need to create a function that returns a random integer between two given numbers (inclusive). For example, if we want a random number between 1 and 5, the possible outputs are: 1, 2, 3, 4, or 5. The Math.random() Method Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive). We need to transform this to get integers in our desired ... Read More

Find the largest palindrome number made from the product of two n digit numbers in JavaScript

Nikitasha Shrivastava
Updated on 15-Mar-2026 23:19:00

406 Views

In this problem, we need to find the largest palindrome number that can be created by multiplying two n-digit numbers using JavaScript. We'll implement two functions: one to find the largest palindrome product and another to check if a number is palindromic. What are Palindrome Numbers? A palindrome number reads the same forwards and backwards. Examples include 44, 121, 202, 404, and 909. When reversed, these numbers remain identical to their original form. Understanding the Problem We need to find the largest possible palindrome that results from multiplying two n-digit numbers. For example, with 2-digit numbers ... Read More

Finding the length of the diagonal of a cuboid using JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

301 Views

We need to write a JavaScript function that calculates the diagonal length of a cuboid (rectangular box) given its length, width, and height dimensions. Problem We are required to write a JavaScript function that takes in the length, width and height of a cuboid and return the length of its diagonal. Formula The space diagonal of a cuboid is calculated using the 3D Pythagorean theorem: diagonal = √(length² + width² + height²) Example Following is the code: const height = 10; const width = 12; const length = 15; ... Read More

How to disable the centered scaling of Circle using FabricJS?

Rahul Gurung
Updated on 15-Mar-2026 23:19:00

312 Views

In this tutorial, we are going to learn how to disable the centered scaling of Circle using FabricJS. Circle is one of the various shapes provided by FabricJS. In order to create a circle, we will have to create an instance of fabric.Circle class and add it to the canvas. When being scaled via controls, assigning a true value to the centeredScaling property, uses the center as the object's origin of transformation. Syntax new fabric.Circle({ centeredScaling: Boolean }: Object) Parameters options (optional) − ... Read More

How to add shadow to a Triangle using FabricJS?

Rahul Gurung
Updated on 15-Mar-2026 23:19:00

186 Views

In this tutorial, we are going to learn how to add a shadow to a Triangle using FabricJS. Triangle is one of the various shapes provided by FabricJS. In order to create a triangle, we will have to create an instance of fabric.Triangle class and add it to the canvas. Our triangle object can be customized in various ways like changing its dimensions, adding a background color or even adding a shadow to it. We can add a shadow to the triangle by using the shadow property. Syntax new fabric.Triangle({ shadow : fabric.Shadow }: Object) ... Read More

How to lock the vertical scaling of Textbox using FabricJS?

Rahul Gurung
Updated on 15-Mar-2026 23:19:00

302 Views

In this tutorial, we are going to learn how to lock the vertical scaling of a Textbox using FabricJS. Just as we can specify the position, colour, opacity and dimension of a textbox object in the canvas, we can also specify whether we want to stop scaling an object vertically. This can be done by using the lockScalingY property. Syntax new fabric.Textbox(text: String, { lockScalingY : Boolean }: Object) Parameters text − This parameter accepts a String which is the text string that we want to display inside our ... Read More

How to center an object vertically with respect to current viewport of canvas in IText using FabricJS?

Rahul Gurung
Updated on 15-Mar-2026 23:19:00

345 Views

In this tutorial, we are going to learn about how to center an object vertically with respect to current viewport of canvas in IText using FabricJS. The IText class was introduced in FabricJS version 1.4, extends fabric.Text and is used to create IText instances. An IText instance gives us the freedom to select, cut, paste or add new text without additional configurations. There are also various supported key combinations and mouse/touch combinations which make text interactive which are not provided in Text. Textbox, however, which is based on IText allows us to resize the text rectangle and wraps lines ... Read More

How to solve too many try catch in Typescript?

Shubham Vora
Updated on 15-Mar-2026 23:19:00

1K+ Views

We can use the try-catch statements to handle errors in TypeScript. Sometimes, we require adding more than one try-catch block in the code to handle multiple errors. When we add multiple try-catch statements in the code, the code becomes unreadable, and it becomes a headache for developers to refactor. In this tutorial, we will learn to convert too many try-catch blocks into a single try-catch block which can manage multiple errors. Syntax Users can follow the syntax below to use the single try-catch block in TypeScript. try { throw new Error("error_message"); ... Read More

Advertisements