AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 397 of 840

Replace() with Split() in JavaScript to append 0 if number after comma is a single digit

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 345 Views

When working with decimal numbers as strings, you often need to format them properly. This article shows how to append a zero to single-digit numbers after a comma using JavaScript's split() and replace() methods. Problem Statement Given a string like "250, 5", we need to: Append a zero if the number after the comma is a single digit Return -1 if the string contains more than one comma Solution Using split() and replace() We can combine split() and replace() methods to achieve this: const a = "250, 5"; const roundString = (str) ...

Read More

How to modify key values in an object with JavaScript and remove the underscore?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 618 Views

In JavaScript, you can modify object keys to remove underscores and convert them to camelCase using regular expressions combined with Object.fromEntries() and Object.entries(). Syntax const camelCaseKey = str => str.replace(/(_)(.)/g, (_, __, char) => char.toUpperCase()); const newObject = Object.fromEntries( Object.entries(originalObject).map(([key, value]) => [camelCaseKey(key), value]) ); Example // Function to convert underscore keys to camelCase var underscoreSpecifyFormat = str => str.replace(/(_)(.)/g, (_, __, v) => v.toUpperCase()); // Original object with underscore keys var JsonObject = { first_Name_Field: 'John', last_Name_Field: ...

Read More

Finding duplicate "words" in a string - JavaScript

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

We need to write a JavaScript function that takes a string and returns only the words that appear more than once in the original string. For example, if the input string is: const str = "big black bug bit a big black dog on his big black nose"; Then the output should be: "big black" Solution Using indexOf() and lastIndexOf() We can identify duplicates by comparing the first and last occurrence positions of each word: const str = "big black bug bit a big black dog on his ...

Read More

Finding the sum of unique array values - JavaScript

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

We are required to write a JavaScript function that takes in an array of numbers that may contain some duplicate numbers. Our function should return the sum of all the unique elements (elements that only appear once in the array) present in the array. For example, if the input array is: const arr = [2, 5, 5, 3, 2, 7, 4, 9, 9, 11]; Then the output should be 25 (3 + 7 + 4 + 11 = 25), as these are the only elements that appear exactly once. Using indexOf() and lastIndexOf() ...

Read More

How to convert array to object in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 443 Views

Converting arrays to objects is a common task in JavaScript. There are several approaches depending on your data structure and requirements. Method 1: Array of Arrays to Objects with Alphabetic Keys Let's convert a nested array structure into objects with keys as English alphabet letters: const data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]; const dataArr = data.map(arr => { return arr.reduce((acc, cur, index) => ({ ...acc, [String.fromCharCode(97 + index)]: ...

Read More

Remove extra spaces in string JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 839 Views

To remove extra spaces from strings in JavaScript, you can use several methods depending on your needs. Here are the most common approaches. Remove All Spaces To remove all spaces from a string, use the replace() method with a regular expression: var sentence = "My name is John Smith "; console.log("Original string:"); console.log(sentence); var noSpaces = sentence.replace(/\s+/g, ''); console.log("After removing all spaces:"); console.log(noSpaces); Original string: My name is John Smith After removing all spaces: MynameisJohnSmith Remove Leading and Trailing Spaces Use trim() to remove spaces only from the ...

Read More

Finding roots of a quadratic equation – JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 603 Views

A quadratic equation has the form ax² + bx + c = 0, where a, b, and c are coefficients. To find the roots, we use the quadratic formula and check if the discriminant is non-negative for real roots. Quadratic Formula The quadratic formula is: x = (-b ± √(b² - 4ac)) / (2a) The discriminant (b² - 4ac) determines the nature of roots: If discriminant > 0: Two distinct real roots If discriminant = 0: One repeated real root If discriminant < 0: No real roots (complex roots) Example ...

Read More

Get filename from string path in JavaScript?

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

We need to write a function that takes in a string file path and returns the filename. Filename usually lives right at the very end of any path, although we can solve this problem using regex but there exists a simpler one-line solution to it using the string split() method of JavaScript and we will use the same here. Let's say our file path is − "/app/base/controllers/filename.js" Using split() Method Following is the code to get file name from string path − const filePath = "/app/base/controllers/filename.js"; const extractFilename = (path) => { ...

Read More

Nearest power 2 of a number - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 911 Views

We need to write a JavaScript function that takes a number and returns the nearest power of 2. A power of 2 is any number that can be expressed as 2n where n is a whole number (1, 2, 4, 8, 16, 32, 64, 128, 256, etc.). For example, if the input is 365, the output should be 256 because 256 (28) is closer to 365 than the next power of 2, which is 512 (29). Algorithm Explanation The algorithm works by: Starting with base = 1 (20) Doubling the base until it exceeds the input ...

Read More

Store count of digits in order using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 177 Views

When working with strings containing digits, you often need to count how many times each digit appears. This is a common programming task that can be solved efficiently using JavaScript objects. Suppose we have a string with digits like this: const str = '11222233344444445666'; We need to write a JavaScript function that takes this string and returns an object representing the count of each digit in the string. For this string, the expected output should be: { "1": 2, "2": 4, "3": 3, "4": ...

Read More
Showing 3961–3970 of 8,392 articles
« Prev 1 395 396 397 398 399 840 Next »
Advertisements