Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 402 of 840
JavaScript JSON HTML
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 MoreJavaScript code to find the coordinates of every link in a page
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 MoreCombine unique items of an array of arrays while summing values - JavaScript
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 MoreJavaScript - Check if array is sorted (irrespective of the order of sorting)
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 MoreNormalize numbers in an object - JavaScript
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 MoreAlert for unsaved changes in form in JavaScript
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 MoreStrange syntax, what does `?.` mean in JavaScript?
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 MoreReplace HTML div into text element with JavaScript?
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 MoreChange string based on a condition - JavaScript
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 MoreWhat is this problem in JavaScript's selfexecuting anonymous function?
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