AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 483 of 840

Vowel, other characters and consonant difference in a string JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 244 Views

We are required to write a function that takes in a string of definite characters and the function should return the difference between the count of vowels plus other characters and consonants in the string. Problem Explanation For example, if the string is: "HEllo World!!" Then, we have 7 consonants, 3 vowels and 3 other characters here so the output should be: |7 - (3+3)| = 1 Hence, the output should be 1 Example const str = 'HEllo World!!'; const findDifference = str => { const creds ...

Read More

Split Space Delimited String and Trim Extra Commas and Spaces in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 459 Views

When working with strings that contain multiple commas and spaces, you can use regular expressions with split() and join() methods to clean them up effectively. The Problem Consider this messy string with multiple consecutive commas and spaces: var sentence = "My, , , , , , , Name, , , , is John ,, , Smith"; console.log("Original string:", sentence); Original string: My, , , , , , , Name, , , , is John ,, , Smith Solution: Using Regular Expression with split() and join() The split(/[\s, ]+/) method splits ...

Read More

Finding difference of greatest and the smallest digit in a number - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 439 Views

We are required to write a JavaScript function that takes in a number and returns the difference between the greatest and the smallest digit present in it. For example: If the number is 5464676, then the smallest digit here is 4 and the greatest is 7 Hence, our output should be 3 Example Let's write the code for this function — const num = 44353456; const difference = (num, min = Infinity, max = -Infinity) => { if(num){ const digit = num ...

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

Return the difference between the maximum & minimum number formed out of the number n in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 429 Views

We have to write a function that takes a positive number n and returns the difference between the maximum number and the minimum number that can be formed from its digits. For example, if the number n is 203: The maximum number that can be formed from its digits will be 320 The minimum number that can be formed from its digits will be 23 (placing the zero at one's place) And the difference will be: 320 - 23 = 297 Therefore, the output should ...

Read More

Attach event to dynamic elements in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 251 Views

To attach events to dynamic elements in JavaScript, use event delegation with document.addEventListener(). This technique listens on a parent element (like document) and handles events from child elements that may be created dynamically. Why Event Delegation? When elements are added to the DOM dynamically (after the page loads), direct event listeners won't work because they weren't attached when the element was created. Event delegation solves this by using event bubbling. Basic Event Delegation Example Dynamic Events Example ...

Read More

Object to array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 373 Views

Converting an object to an array of key-value pairs is a common task in JavaScript. There are several built-in methods to achieve this transformation. Sample Object Let's start with this example object: const obj = { name: "Vikas", age: 45, occupation: "Frontend Developer", address: "Tilak Nagar, New Delhi", experience: 23, salary: "98000" }; Using Object.entries() (Recommended) The simplest approach is using Object.entries(), which directly converts an object to an ...

Read More

Removing redundant elements from array altogether - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 378 Views

We are required to write a function that takes in an array and returns a new array that have all duplicate values removed from it. The values that appeared more than once in the original array should not even appear for once in the new array. For example, if the input is: const arr = [763, 55, 43, 22, 32, 43, 763, 43]; The output should be: const output = [55, 22, 32]; Understanding the Approach We will be using the following two methods: ...

Read More

No. of ways to empty an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 260 Views

JavaScript provides several methods to empty an array. Each method has different use cases and behaviors, especially when multiple references to the same array exist. Method 1: Setting to New Array This method creates a new empty array and assigns it to the variable. However, it doesn't affect other references to the original array. Setting to New Array let arr = [1, 2, 3, ...

Read More

Solution to the clumsy factorial problem in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 420 Views

Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n. For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + ...

Read More
Showing 4821–4830 of 8,392 articles
« Prev 1 481 482 483 484 485 840 Next »
Advertisements