AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 398 of 840

JavaScript BOM Window Screen

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 535 Views

The JavaScript BOM (Browser Object Model) Window screen object provides detailed information about the user's display screen. This object is useful for creating responsive web applications and gathering display specifications. Screen Object Properties Property Description screen.width Returns the total screen width in pixels ...

Read More

How to set attribute in loop from array JavaScript?

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

Let's say we are required to write a function that takes in an array and changes the id attribute of first n divs present in a particular DOM according to corresponding values of this array, where n is the length of the array. We will first select all divs present in our DOM, iterate over the array we accepted as one and only argument and assign the corresponding id to each div. Basic Example Here's the HTML structure we'll work with: Setting Attributes in Loop ...

Read More

Un-nesting array of object in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 341 Views

To un-nest an array of objects in JavaScript, you can use the map() method to restructure nested data into a flatter format. This is useful when you have deeply nested objects and want to extract specific properties to create a simpler structure. Problem: Nested Object Structure Let's say you have an array of student objects where subject details are nested inside each student: const studentDetails = [ { "studentId": 101, "studentName": "John", ...

Read More

Finding area of triangle in JavaScript using Heron's formula

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 965 Views

We are given the lengths of three sides of a triangle and we are required to write a function that returns the area of the triangle using the length of its sides. Heron's Formula We can calculate the area of a triangle if we know the lengths of all three sides, using Heron's formula: Step 1 − Calculate "s" (half of the triangle's perimeter): s = (a + b + c) / 2 Step 2 − Then calculate the Area using Heron's formula: A = √(s(s-a)(s-b)(s-c)) Syntax ...

Read More

JavaScript - Get the text of a span element

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

In JavaScript, you can get the text content of a span element using several methods. The most common approaches are innerHTML, textContent, and innerText. Methods to Get Span Text There are three main properties to retrieve text from a span element: innerHTML - Gets HTML content including tags textContent - Gets plain text content (recommended) innerText - Gets visible text content Example: Getting Span Text Get Span Text ...

Read More

Why does Array.map(Number) convert empty spaces to zeros? JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 464 Views

When using Array.map(Number) on a string containing spaces, JavaScript converts empty spaces to zeros. This happens due to how the Number() constructor handles string-to-number conversion. The Problem Let's examine this behavior with a practical example: const digify = (str) => { const parsedStr = [...str].map(Number) return parsedStr; } console.log(digify("778 858 7577")); [ 7, 7, 8, 0, 8, 5, 8, 0, 7, 5, 7, 7 ] Notice how the spaces in the string are converted to 0 instead ...

Read More

Remove characters from a string contained in another string with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 426 Views

When working with strings in JavaScript, you might need to remove all characters from one string that appear in another string. This can be achieved using the replace() method combined with reduce(). Problem Statement Given two strings, we want to remove all characters from the first string that exist in the second string: var originalName = "JOHNDOE"; var removalName = "JOHN"; // Expected result: "DOE" Solution Using replace() and reduce() The reduce() method iterates through each character in the removal string, and replace() removes the first occurrence of that character from the original ...

Read More

Largest and smallest word in a string - JavaScript

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

We need to write a JavaScript function that takes a string and returns an array containing the smallest and largest words from the string based on their length. For example, if we have the string: const str = "Hardships often prepare ordinary people for an extraordinary destiny"; The output should be: const output = ["an", "extraordinary"]; The word "an" has 2 characters (smallest) and "extraordinary" has 13 characters (largest). Example Here's the complete implementation: const str = "Hardships often prepare ordinary people for an extraordinary destiny"; ...

Read More

JavaScript example for Capturing mouse positions after every given interval

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 406 Views

In this article, we will demonstrate how to capture the mouse position in JavaScript at regular intervals and log or use this data for various purposes. Capturing mouse positions after every given interval refers to a program or functionality where the current position of the mouse pointer is tracked at regular time intervals. This can be useful in scenarios like debugging, interactive applications, or visualizing user movement on a web page. Use Cases Imagine you want to monitor user activity on a web page and record the mouse position every second. This can be useful for: ...

Read More

Return Largest Numbers in Arrays passed using reduce method?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 208 Views

To find the largest number in each array using the reduce() method, combine it with Math.max() and the spread operator. This approach processes multiple arrays and returns an array of maximum values. Syntax array.reduce((accumulator, currentArray) => { accumulator.push(Math.max(...currentArray)); return accumulator; }, []); Example const getBiggestNumberFromArraysPassed = allArrays => allArrays.reduce( (maxValue, maxCurrent) => { maxValue.push(Math.max(...maxCurrent)); return maxValue; }, [] ); console.log(getBiggestNumberFromArraysPassed([[45, ...

Read More
Showing 3971–3980 of 8,392 articles
« Prev 1 396 397 398 399 400 840 Next »
Advertisements