Javascript Articles

Page 452 of 534

Map an integer from decimal base to hexadecimal with custom mapping JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 437 Views

Usually when we convert a decimal to hexadecimal (base 16) we use the set 0123456789ABCDEF to map the number. We are required to write a function that does exactly the same but provides user the freedom to use any scale rather than the one mentioned above. For example − The hexadecimal notation of the decimal 363 is 16B But if the user decides to use, say, a scale 'qwertyuiopasdfgh' instead of '0123456789ABCDEF', the number 363, then will be represented by wus That's what we are required to do. So, let's do this by ...

Read More

How to return a random number between 1 and 200 with JavaScript?

Manikanth Mani
Manikanth Mani
Updated on 15-Mar-2026 2K+ Views

To return a random number between 1 and 200, use JavaScript's Math.random() and Math.floor() methods. How It Works Math.random() generates a decimal between 0 (inclusive) and 1 (exclusive). To get integers from 1 to 200: Multiply by 200 to get 0 to 199.999... Use Math.floor() to round down to 0-199 Add 1 to shift the range to 1-200 Example You can try to run the following code to return a random number in JavaScript. ...

Read More

Find longest string in array (excluding spaces) JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 228 Views

We are required to write a function that accepts an array of string literals and returns the index of the longest string in the array. While calculating the length of strings we don't have to consider the length occupied by whitespaces. If two or more strings have the same longest length, we have to return the index of the first string that does so. We will iterate over the array, split each element by whitespace, join again and calculate the length. We'll track the maximum length and its index, updating when we find a longer string. Syntax ...

Read More

Sort array by year and month JavaScript

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

Sorting arrays of objects by multiple criteria is a common requirement in JavaScript applications. In this tutorial, we'll learn how to sort an array by year first, then by month for objects with the same year. The Problem We have an array of objects containing year and month properties: const arr = [{ year: 2020, month: 'January' }, { year: 2017, month: 'March' }, { year: 2010, month: 'January' }, { ...

Read More

Digital root sort algorithm JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 301 Views

Digital root of a positive integer is defined as the sum of all of its digits. We are given an array of integers and need to sort it based on digit root values. If a number comes before b, then the digit root of a should be less than or equal to the digit root of b. If two numbers have the same digit root, the smaller number should come first. For example, 4 and 13 have the same digit root (4 and 1+3=4), but since 4 < 13, the number 4 comes before 13 in the sorted array. ...

Read More

Convert text to lowercase with CSS

Samual Sam
Samual Sam
Updated on 15-Mar-2026 4K+ Views

To convert text to lowercase with CSS, we will be using the lowercase value of text-transform property. Syntax selector { text-transform: lowercase; } Example In this example, we are using text-transform property and displaying the result as before and after the conversion using p tag. #lcase { text-transform: lowercase; } ...

Read More

Returning an array with the minimum and maximum elements JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 186 Views

Finding the minimum and maximum elements in a JavaScript array is a common task. This tutorial shows how to return both values in a single array using different approaches. const arr = [12, 54, 6, 23, 87, 4, 545, 7, 65, 18, 87, 8, 76]; We need to write a function that finds the minimum and maximum elements and returns them as an array with minimum at index 0 and maximum at index 1. Using Array.reduce() Method The reduce() method provides an efficient way to traverse the array once and track both minimum and ...

Read More

Fill missing numeric values in a JavaScript array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 575 Views

We are given an array of n entries, of which only 2 are Numbers, all other entries are null. Something like this: const arr = [null, null, -1, null, null, null, -3, null, null, null]; We are supposed to write a function that takes in this array and complete the arithmetic series of which these two numbers are a part. To understand this problem more clearly, we can think of these null values as blank space where we need to fill numbers so that the whole array forms an arithmetic progression. About Arithmetic Progression ...

Read More

Sum from array values with similar key in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 599 Views

When working with arrays of data, you often need to group and sum values based on a common key. This article demonstrates how to sum array values with similar keys using JavaScript's reduce() method. Let's say we have an array containing stock transaction data for a company over time: const transactions = [ ['AAPL', 'buy', 100], ['WMT', 'sell', 75], ['MCD', 'buy', 125], ['GOOG', 'sell', 10], ['AAPL', 'buy', 100], ['AAPL', 'sell', 100], ...

Read More

How to splice duplicate item in array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 711 Views

We have an array of Number / String literals that contain some duplicate values, we have to remove these values from the array without creating a new array or storing the duplicate values anywhere else. We will use the Array.prototype.splice() method to remove entries in-place, and we will take help of Array.prototype.indexOf() and Array.prototype.lastIndexOf() method to determine the duplicacy of any element. The Problem with Forward Iteration When using forEach() to remove duplicates, we encounter index shifting issues. Here's why the basic approach doesn't work perfectly: const arr = [1, 4, 6, 1, 2, 5, ...

Read More
Showing 4511–4520 of 5,340 articles
« Prev 1 450 451 452 453 454 534 Next »
Advertisements