Articles on Trending Technologies

Technical articles with clear explanations and examples

Summing up digits and finding nearest prime in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 162 Views

We are required to write a JavaScript function that takes in a number, finds the sum of its digits and returns a prime number that is just greater than or equal to the sum. This problem involves two main steps: calculating the digit sum and finding the nearest prime number greater than or equal to that sum. Breaking Down the Solution Our solution requires three functions: digitSum() - calculates the sum of all digits in a number isPrime() - checks if a number is prime nearestPrime() - finds the nearest prime >= digit sum ...

Read More

Sum of array object property values in new array of objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 769 Views

When working with arrays of objects, we often need to group objects by a common property and sum up their numeric values. This is particularly useful when dealing with student records, sales data, or any scenario where duplicate categories need to be consolidated. Suppose we have an array of objects that contains data about students and their marks: const arr = [ { subject: 'Maths', marks: '40', noOfStudents: '5' }, { subject: 'Science', marks: '50', noOfStudents: '16' }, { subject: 'History', marks: '35', noOfStudents: '23' }, { subject: 'Science', ...

Read More

Making array unique in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 294 Views

In JavaScript, making array elements unique by incrementing duplicates is a common problem. This approach finds the minimum number of incremental moves needed to ensure all array elements are unique. Problem Statement Given an array of numbers, we need to find the minimum number of moves to make all elements unique. A move consists of incrementing any element by 1. For example, with the array [12, 15, 7, 15], we need 1 move to make it unique by changing one 15 to 16. Algorithm Approach The strategy is to sort the array first, then iterate ...

Read More

Appending Suffix to Numbers in JavaScript

Revathi Satya Kondra
Revathi Satya Kondra
Updated on 15-Mar-2026 1K+ Views

Appending suffixes to numbers in JavaScript is used to add ordinal indicators (like "st", "nd", "rd", and "th") to the end of a number to denote its position in a sequence. This is useful for displaying dates, rankings, or other numeric data. A custom function can be created to append the correct suffix to a given number. The function handles special cases for numbers ending in 11, 12, and 13, and applies the appropriate suffix based on the last digit for other numbers. Rules for Ordinal Suffixes ...

Read More

How to lock the horizontal scaling of Ellipse using FabricJS?

Rahul Gurung
Rahul Gurung
Updated on 15-Mar-2026 234 Views

In this tutorial, we are going to learn how to lock the horizontal 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 scaling an object horizontally. This can be done by using the lockScalingX property. Syntax new fabric.Ellipse({ lockScalingX : Boolean }: Object) Parameters options (optional) − This parameter is an Object which provides additional customizations to our ellipse. Using this parameter color, cursor, stroke ...

Read More

How can a page be forced to load another page in JavaScript?

Tarun Singh
Tarun Singh
Updated on 15-Mar-2026 7K+ Views

In JavaScript, you can force a page to load another page using the window.location object. This object provides three main approaches: window.location.href property, window.location.assign() method, and window.location.replace() method. Each handles browser history differently, making them suitable for different use cases. Using window.location.href The window.location.href property contains the current URL and can redirect users to a new page. This is the most commonly used method for page redirection. Syntax window.location.href = "new_url"; You can also use setTimeout() to redirect after a delay: Delayed Redirect Example ...

Read More

Taking common elements from many arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 562 Views

We are required to write a JavaScript function that takes in any arbitrary number of arrays and returns an array of elements that are common to all arrays. If there are no common elements, then we should return an empty array. This problem requires finding the intersection of multiple arrays, which means identifying elements that exist in every provided array. Understanding the Problem The intersection of arrays means finding elements that appear in all arrays. For example, if we have arrays [1, 2, 3], [2, 3, 4], and [3, 4, 5], the common element is 3. ...

Read More

How to round up to the nearest N in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

In JavaScript, rounding a number to the nearest multiple of N requires dividing by N, rounding the result, then multiplying back by N. Consider this example: const num = 76; When rounding to different factors: Round to nearest 10: 76 becomes 80 Round to nearest 100: 76 becomes 100 Round to nearest 1000: 76 becomes 0 We need a JavaScript function that takes a number and a rounding factor, returning the rounded result. Syntax const roundOffTo = (num, factor = 1) => { const ...

Read More

Checking for centrally peaked arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 160 Views

We are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument. Our function should check whether the input array is a centrally peaked array or not. If it is a centrally peaked array, we should return true, false otherwise. Conditions for Centrally Peaked Array The conditions for being a centrally peaked array are: arr.length >= 3 There exists some i with 0 < i < arr.length - 1 such that: ...

Read More

Checking if a string contains all unique characters using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 482 Views

Problem We are required to write a JavaScript function that takes in a string and returns true if all the characters in the string appear only once and false otherwise. Method 1: Using indexOf() and lastIndexOf() This approach compares the first and last occurrence of each character. If they differ, the character appears multiple times. const str = 'thisconaluqe'; const allUnique = (str = '') => { for(let i = 0; i < str.length; i++){ const el = str[i]; ...

Read More
Showing 14721–14730 of 61,297 articles
Advertisements