AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 340 of 840

Converting numbers to Indian currency using JavaScript

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

Converting numbers to Indian currency format is a common requirement in web applications. JavaScript provides a built-in method to format numbers according to different locales, including the Indian numbering system. Understanding Indian Currency Format Indian currency uses a unique grouping system where numbers are grouped by hundreds (lakhs and crores) rather than thousands. For example: 1000 → ₹1, 000.00 129943 → ₹1, 29, 943.00 76768798 → ₹7, 67, 68, 798.00 Using toLocaleString() Method The toLocaleString() method with Indian locale ('en-IN') automatically handles the currency formatting: const num1 = 1000; const num2 ...

Read More

Checking for uniqueness in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 242 Views

We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument. The function should return true if all the numbers in the array appear only once (i.e., all the numbers are unique), and false otherwise. For example − If the input array is − const arr = [12, 45, 6, 34, 12, 57, 79, 4]; Then the output should be − const output = false; because the number 12 appears twice in the array. Method 1: Using indexOf() ...

Read More

Deep count of elements of an array using JavaScript

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

We are required to write a JavaScript function that takes in a nested array of elements and returns the deep count of all elements present in the array, including those in nested subarrays. Problem Given a nested array, we need to count all elements at every level of nesting. Input const arr = [1, 2, [3, 4, [5]]]; Output const output = 7; Because the elements at level 1 are 2, elements at level 2 are 2 and elements at level 3 are 1. Hence the deep count is 7. Using Recursive Reduce Method ...

Read More

Alternating sum of elements of a two-dimensional array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 415 Views

Problem We are required to write a JavaScript function that takes in a 2-Dimensional array of m X n order of numbers. For this array, our function should calculate the alternating sum where each element is multiplied by (-1)^(i+j), where i and j are the row and column indices respectively. The mathematical formula is: $\sum_{i=1}^m \sum_{j=1}^n (-1)^{i+j}a_{ij}$ How It Works The alternating sum works by applying a positive or negative sign to each element based on its position. When the sum of row index (i) and column index (j) is even, the element gets a positive sign. When ...

Read More

How to access object properties from result returned by async() function in JavaScript?

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

In this article, you will understand how to access object properties from result returned by async() functions in JavaScript. An object property in JavaScript is a variable that is associated with the object itself, i.e. the properties have a name and value is one of the attributes linked with the property. When working with async functions that return objects, you can access their properties using standard JavaScript notation once the promise resolves. The key is using await to wait for the promise to resolve before accessing properties. Using Dot Notation In this example, let's understand how to ...

Read More

How to make a setInterval() stop after some time or after a number of actions in JavaScript?

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

The setInterval() function repeatedly executes code at specified intervals. To stop it after some time or a number of actions, use clearInterval() with conditions. Method 1: Stop After Time Duration This example stops the interval after 10 seconds: Stop setInterval After Time var startTime = new Date().getTime(); ...

Read More

Sorting an array by date in JavaScript

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

Sorting an array by date in JavaScript is a common task when working with arrays of objects containing date information. This article shows how to sort objects by their date properties using JavaScript's built-in sorting methods. The Problem Suppose we have an array of objects like this: const arr = [ {id: 1, date: 'Mar 12 2012 10:00:00 AM'}, {id: 2, date: 'Mar 8 2012 08:00:00 AM'} ]; We need to sort this array according to the date property of each object, either newest first ...

Read More

Finding the largest non-repeating number in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 447 Views

In JavaScript, finding the largest non-repeating number in an array requires tracking element frequencies and identifying unique values. This problem becomes efficient when we know the value range constraints. We need to write a function that takes an array of integers and returns the largest number that appears exactly once. If no unique number exists, return -1. The constraint given is that all array elements satisfy: 0 < arr[i] < 101 This means all values are between 1 and 100, inclusive. Example Input and Output For the input array: const ...

Read More

Converting humanYears into catYears and dogYears in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 367 Views

We need to write a JavaScript function that converts human age into equivalent cat and dog years. The conversion follows specific aging rules for the first two years, then a constant rate afterward. Problem Statement Create a function that takes human age in years and returns an array containing human years, cat years, and dog years. Input: const humanYears = 15; Expected Output: [15, 76, 89] Age Conversion Rules The aging calculation follows these rules: Year 1: Both cats and dogs age ...

Read More

Summing numbers present in a string separated by spaces using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 701 Views

We are required to write a JavaScript function that takes in a string which has integers inside it separated by spaces. The task of our function is to convert each integer in the string into an integer and return their sum. Problem Given a string containing numbers separated by spaces, we need to: Split the string into individual number strings Convert each string to a number Calculate the sum of all numbers Method 1: Using split() and reduce() const str = '1 5 ...

Read More
Showing 3391–3400 of 8,392 articles
« Prev 1 338 339 340 341 342 840 Next »
Advertisements