AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 432 of 840

Sort Array of numeric & alphabetical elements (Natural Sort) JavaScript

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

We have an array that contains some numbers and some strings. We are required to sort the array such that the numbers get sorted and placed before every string, and then the strings should be placed sorted alphabetically. For example, this array: const arr = [1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9]; console.log("Original array:", arr); Original array: [ 1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9 ] Should look like this after sorting: [1, 6, 7, 9, 47, 'afv', 'bdf', 'fdf', 'svd'] Implementation ...

Read More

Sort a JavaScript array so the NaN values always end up at the bottom.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 661 Views

We have an array that contains String and number mixed data types, we have to write a sorting function that sorts the array so that the NaN values always end up at the bottom. The array should contain all the normal numbers first followed by string literals and then followed by NaN numbers. We know that the data type of NaN is "number", so we can't check for NaN like !number && !string. Moreover, if we simply check the tautology and falsity of elements then empty strings will also satisfy the same condition which NaN or undefined satisfies. ...

Read More

Add class (odd and even) in HTML via JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 679 Views

JavaScript provides several ways to add CSS classes to HTML elements based on odd and even positions. You can use CSS nth-child selectors or JavaScript to dynamically add classes to elements. Method 1: Using CSS nth-child Selectors The simplest approach is using CSS nth-child(odd) and nth-child(even) selectors to style elements directly: Odd Even Classes .subjectName:nth-child(odd) { ...

Read More

How to join two arrays in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 316 Views

In JavaScript, there are several methods to join two arrays together. The most common approaches are using the concat() method and the spread operator. Using concat() Method The concat() method creates a new array by merging two or more arrays without modifying the original arrays. Join Arrays with concat() Join Arrays using concat() Join Arrays ...

Read More

Pad a string using random numbers to a fixed length using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 440 Views

We need to write a function that takes a string and a target length, then pads the string with random numbers until it reaches the specified length. This is useful for creating fixed-length identifiers or codes. How It Works The function uses recursion to add random digits (0-9) to the end of the string until it reaches the target length. Each recursive call generates a new random digit using Math.random(). Implementation const padString = (str, len) => { if(str.length < len){ const random ...

Read More

Why can't my HTML file find the JavaScript function from a sourced module?

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

When an HTML file cannot find JavaScript functions from a sourced module, it's usually because the function isn't properly exported from the module or the import statement is incorrect. ES6 modules require explicit export/import statements to share functions between files. The Export Problem Functions in JavaScript modules are not globally accessible unless explicitly exported. Without the export keyword, functions remain private to the module. demo.js console.log("function will import"); export function test(){ console.log("Imported!!!"); } Example: Complete Module Import index.html ...

Read More

How to add, access, delete, JavaScript object properties?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 206 Views

JavaScript objects are collections of key-value pairs where you can dynamically add, access, and delete properties. This guide demonstrates the fundamental operations for managing object properties. Syntax // Adding properties obj.propertyName = value; obj['propertyName'] = value; // Accessing properties obj.propertyName; obj['propertyName']; // Deleting properties delete obj.propertyName; delete obj['propertyName']; Example: Adding Properties Object Properties Add, Access, Delete JavaScript Object Properties Manage Properties function manageProperties() { let obj = { firstName: ...

Read More

Joining a JavaScript array with a condition?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 662 Views

Joining a JavaScript array with a condition involves filtering elements that meet specific criteria and then combining them into a string. This is achieved by chaining the filter() method with the join() method. Syntax array.filter(condition).join(separator) Parameters condition - A function that tests each element separator - String used to separate elements (default is comma) Example: Joining Even Numbers Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...

Read More

How to Replace null with "-" JavaScript

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

In JavaScript, you often need to replace null, undefined, empty strings, and other falsy values with a default placeholder like "-". This is common when displaying data in tables or forms where empty values should show a dash instead of being blank. Understanding Falsy Values JavaScript considers these values as falsy: null, undefined, '' (empty string), 0, NaN, and false. We can use this to our advantage when replacing them. Method 1: Using Object.keys() and forEach() This approach iterates through all object keys and replaces falsy values in place: const obj = { ...

Read More

How to transform object of objects to object of array of objects with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 840 Views

To transform an object of objects into an object of array of objects, you can use Object.fromEntries() along with Object.entries() and map(). This transformation wraps each nested object in an array while preserving the original keys. Syntax Object.fromEntries( Object.entries(originalObject).map(([key, value]) => [key, [value]]) ) Example const studentDetails = { 'details1': {Name: "John", CountryName: "US"}, 'details2': {Name: "David", CountryName: "AUS"}, 'details3': {Name: "Bob", CountryName: "UK"}, }; const transformedObject = Object.fromEntries( Object.entries(studentDetails).map(([key, value]) => ...

Read More
Showing 4311–4320 of 8,392 articles
« Prev 1 430 431 432 433 434 840 Next »
Advertisements