AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 402 of 840

JavaScript JSON HTML

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 325 Views

Generating HTML from JSON data is a common task in web development. You can fetch JSON data from APIs and dynamically create HTML elements to display the information. Note − JSONPlaceholder is a fake Online REST API for Testing and Prototyping. Example: Creating HTML Table from JSON JSON to HTML Example body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...

Read More

JavaScript code to find the coordinates of every link in a page

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 239 Views

JavaScript provides several methods to find the position and coordinates of elements on a page. For links, we can use properties like offsetLeft, offsetTop, offsetWidth, and offsetHeight to get their position and dimensions. Understanding Element Coordinates In JavaScript, element coordinates are measured from the top-left corner of the page: offsetLeft - Distance from the left edge of the page offsetTop - Distance from the top edge of the page offsetWidth - Width of the element including padding and borders offsetHeight - Height of the element including padding and borders Example ...

Read More

Combine unique items of an array of arrays while summing values - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 520 Views

We have an array of arrays, where each subarray contains exactly two elements: a string (person name) and an integer (value). Our goal is to combine subarrays with the same first element and sum their second elements. For example, this input array: const example = [ ['first', 12], ['second', 19], ['first', 7] ]; Should be converted to: [ ['first', 19], ['second', 19] ] Solution Using Object Mapping We'll create ...

Read More

JavaScript - Check if array is sorted (irrespective of the order of sorting)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 549 Views

In JavaScript, checking if an array is sorted regardless of order (ascending or descending) requires comparing adjacent elements to determine if they follow a consistent pattern. We need to identify whether the array is sorted in ascending order, descending order, or not sorted at all. Understanding the Problem An array can be considered sorted if all elements follow either: Ascending order: each element is greater than or equal to the previous one Descending order: each element is less than or equal to the previous one Method 1: Check Both Directions This approach checks if ...

Read More

Normalize numbers in an object - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 682 Views

Number normalization scales values from one range to another proportionally. This technique is commonly used in data processing, machine learning, and graphics programming. Problem Definition Given an object with numeric values and a target range [a, b], we need to transform all values so that: The smallest original value becomes a The largest original value becomes b Other values are scaled proportionally between a and b Normalization Formula The formula for linear normalization is: normalized_value = range_min + ((value - original_min) * (range_max - range_min)) / (original_max - original_min) ...

Read More

Alert for unsaved changes in form in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 799 Views

Preventing data loss is crucial in web forms. JavaScript's beforeunload event allows you to warn users when they attempt to leave a page with unsaved changes. Basic Implementation The simplest approach uses the beforeunload event to show a browser confirmation dialog: Form Unsaved Changes Alert body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...

Read More

Strange syntax, what does `?.` mean in JavaScript?

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

The ?. operator is called optional chaining and provides a safe way to access nested object properties without throwing errors when intermediate properties are undefined or null. The Problem with Regular Property Access Consider this nested object describing a person: const being = { human: { male: { age: 23 } } }; console.log(being.human.male.age); 23 This works fine, but what happens if we try to access a property that doesn't exist? ...

Read More

Replace HTML div into text element with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 889 Views

To replace HTML div content with text from another element, you can use document.querySelectorAll() to select multiple elements and getElementsByClassName() to get the source text. This approach allows you to dynamically update multiple elements with content from a single source. Example Replace Div Content My Name is John ...

Read More

Change string based on a condition - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 813 Views

We are required to write a JavaScript function that takes in a string. The task of our function is to change the string according to the following condition − If the first letter in the string is a capital letter then we should change the full string to capital letters. Otherwise, we should change the full string to small letters. Example Following is the code − const str1 = "This is a normal string"; const str2 = "thisIsACamelCasedString"; const changeStringCase = str => { ...

Read More

What is this problem in JavaScript's selfexecuting anonymous function?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 223 Views

Let's analyze this JavaScript code snippet that demonstrates a common confusion with variable hoisting and function declarations in self-executing anonymous functions. var name = 'Zakir'; (() => { name = 'Rahul'; return; console.log(name); function name(){ let lastName = 'Singh'; } })(); console.log(name); Naive Analysis (Incorrect) At first glance, you might expect this sequence: Global variable name is set to 'Zakir' Inside the IIFE, name is ...

Read More
Showing 4011–4020 of 8,392 articles
« Prev 1 400 401 402 403 404 840 Next »
Advertisements