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 419 of 840
Valid triangle edges - JavaScript
In geometry, three lines can form a triangle only if they satisfy the triangle inequality theorem: the sum of any two sides must be greater than the third side. This fundamental rule ensures that the three sides can actually connect to form a closed triangle. For example, if three lines have lengths 4, 9, and 3, they cannot form a triangle because 4 + 3 = 7, which is less than 9. At least one side would be too long to connect with the other two. Triangle Inequality Theorem For sides with lengths a, b, and c ...
Read MoreJavaScript WebAPI File File.name Property
The JavaScript File WebAPI file.name property returns only the name of the file without the path. This property is read-only and provides access to the filename when working with file inputs or drag-and-drop operations. Syntax file.name Return Value Returns a string containing the name of the file without any path information. The name includes the file extension if present. Example File.name Property Example ...
Read MoreNaN and Infinity example in JavaScript
In JavaScript, NaN (Not-a-Number) and Infinity are special numeric values that represent invalid or infinite calculations. Understanding these values is crucial for handling mathematical operations and validation. What is NaN? NaN represents a value that is "Not-a-Number". It occurs when a mathematical operation fails or produces an undefined result. NaN Examples let results = document.getElementById('nanResults'); ...
Read MoreReferring JavaScript function from within itself
In JavaScript, a function can call itself from within its own body, which is known as recursion. This technique is useful for tasks like calculating factorials, traversing nested structures, or repeating operations until a condition is met. What is Recursion? Recursion occurs when a function calls itself. Every recursive function needs two key elements: a base case (condition to stop) and a recursive case (function calling itself with modified parameters). Basic Example: Factorial Function Recursion Example ...
Read MoreRemove duplicates from an array keeping its length same in JavaScript
We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end. For example − If we find 4 duplicate values we have to remove them all and insert four empty strings at the end. Therefore, let's write the code for this problem − Problem Analysis The task requires us to: Remove duplicate elements from the array Keep only the last occurrence of each element Replace removed duplicates ...
Read MoreBest way to find two numbers in an array whose sum is a specific number with JavaScript?
Finding two numbers in an array that sum to a target value is a common programming problem. We'll explore an efficient solution using JavaScript. Problem Statement Given an array of numbers, find two elements whose sum equals a specific target value: var numbers = [10, 3, 40, 50, 20, 30, 100] Target sum = 80 We need to find pairs like (50, 30) where 50 + 30 = 80. Efficient Solution Using Hash Map The optimal approach uses a hash map to store visited numbers and their complements, achieving O(n) time complexity: ...
Read MorePrettify JSON data in textarea input in JavaScript?
JSON data can become difficult to read when it's minified or poorly formatted. JavaScript provides built-in methods to prettify JSON data in textarea elements using JSON.parse() and JSON.stringify(). How It Works The process involves three steps: Parse the JSON string using JSON.parse() to validate and convert it to an object Use JSON.stringify() with spacing parameters to format the object Replace the textarea content with the prettified JSON Example JSON Prettifier ...
Read MoreDistance to nearest vowel in a string - JavaScript
We are required to write a JavaScript function that takes in a string with at least one vowel, and for each character in the string we have to map a number representing its nearest distance from a vowel. For example: If the string is − const str = 'vatghvf'; Then the output should be − const output = [1, 0, 1, 2, 3, 4, 5]; How It Works The algorithm works by: Finding all vowel positions in the string For each character, calculating the minimum distance to any vowel ...
Read MoreJavaScript to generate random hex codes of color
Generating random hex color codes in JavaScript involves creating a 6-character string from hexadecimal digits (0-9, A-F) and prefixing it with '#'. This is useful for creating dynamic color schemes, themes, or visual effects. Understanding Hex Color Codes Hex color codes use the format #RRGGBB where each pair represents red, green, and blue values in hexadecimal (0-F). For example, #FF0000 is pure red, #00FF00 is green, and #0000FF is blue. Method 1: Using Math.random() with Character Selection Random Hex Color Generator ...
Read MoreHow to search for a string in JavaScript?
JavaScript provides several methods to search for strings. The most common approaches are search(), indexOf(), includes(), and match(). Using search() Method The search() method returns the index of the first match or -1 if not found. It supports regular expressions. String Search Example The spring season is about to come. SEARCH FOR 'spring' ...
Read More