AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 419 of 840

Valid triangle edges - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 524 Views

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 More

JavaScript WebAPI File File.name Property

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 143 Views

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 More

NaN and Infinity example in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 270 Views

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 More

Referring JavaScript function from within itself

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

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 More

Remove duplicates from an array keeping its length same in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 251 Views

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 More

Best way to find two numbers in an array whose sum is a specific number with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 534 Views

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 More

Prettify JSON data in textarea input in JavaScript?

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

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 More

Distance to nearest vowel in a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 514 Views

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 More

JavaScript to generate random hex codes of color

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 343 Views

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 More

How to search for a string in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 316 Views

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
Showing 4181–4190 of 8,392 articles
« Prev 1 417 418 419 420 421 840 Next »
Advertisements