Web Development Articles

Page 254 of 801

Finding points nearest to origin in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 715 Views

Problem We need to write a JavaScript function that takes an array of coordinates and a number as arguments. The function should find and return the specified number of closest points to the origin (0, 0) using Euclidean distance. The Euclidean distance between a point (x, y) and the origin is calculated as: √(x² + y²) Example Input and Output For the input: const arr = [[3, 3], [5, -1], [-2, 4]]; const num = 2; The expected output should be: [[3, 3], [-2, 4]] Solution Using Sort ...

Read More

Checking for univalued Binary Search Tree in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 192 Views

A binary search tree is univalued if every node in the tree has the same value. This means all nodes must contain identical data values for the tree to be considered univalued. Problem We need to write a JavaScript function that takes the root of a BST and returns true if the tree is univalued, false otherwise. For example, if the nodes of the tree are: const input = [5, 5, 5, 3, 5, 6]; Then the output should be: const output = false; Binary Search Tree Implementation ...

Read More

Index difference of tuples in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

Problem We are required to write a JavaScript function that takes in an array of integers, arr, as the first and the only argument. Suppose two indices, i and j in the array which satisfy the following conditions − i < j, and arr[i] { let max = 0; const stack = [0]; ...

Read More

Validating push pop sequence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 441 Views

Validating push/pop sequences is a common stack problem where we need to verify if a given pop sequence could result from a specific push sequence on an initially empty stack. Problem Statement Given two arrays pushed and popped containing unique elements, determine if the pop sequence could be achieved from the push sequence using stack operations. const pushed = [1, 2, 3, 4, 5]; const popped = [4, 5, 3, 2, 1]; How It Works The algorithm simulates stack operations by maintaining a stack and two pointers. We push elements until the stack ...

Read More

Making array unique in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 291 Views

In JavaScript, making array elements unique by incrementing duplicates is a common problem. This approach finds the minimum number of incremental moves needed to ensure all array elements are unique. Problem Statement Given an array of numbers, we need to find the minimum number of moves to make all elements unique. A move consists of incrementing any element by 1. For example, with the array [12, 15, 7, 15], we need 1 move to make it unique by changing one 15 to 16. Algorithm Approach The strategy is to sort the array first, then iterate ...

Read More

Finding the nth element of the lucas number sequence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 327 Views

Lucas numbers are a sequence similar to Fibonacci numbers but with different starting values. They follow a specific mathematical pattern where each number is the sum of the two preceding ones. Lucas Number Sequence Definition The Lucas sequence is defined as: L(0) = 2 L(1) = 1 L(n) = L(n-1) + L(n-2) for n ≥ 2 The sequence starts: 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123... Problem Statement We need to write a JavaScript function that takes a number n and returns the nth Lucas number from the ...

Read More

Converting to hex and summing the numeral part in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 332 Views

We are required to write a JavaScript function that takes in a string, converts every character to its hex ASCII value, then sums only the numeric digits (0-9) from the hex representation, ignoring letters (a-f). Problem Given a string, we need to: Convert each character to its ASCII code Convert ASCII codes to hexadecimal Extract only numeric digits from the hex strings Sum all the numeric digits Example Let's see how this works with a complete example: const str = "Hello, World!"; const toHexAndSum = (str = '') => { ...

Read More

Finding quarter based on month index in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 674 Views

We are required to write a JavaScript function that takes in the 1-based month index and return the quarter which the month falls in. Understanding Quarters A year is divided into four quarters, each containing three months: Q1: January, February, March (months 1-3) Q2: April, May, June (months 4-6) Q3: July, August, September (months 7-9) Q4: October, November, December (months 10-12) Method 1: Using if-else Statements const month = 7; const findQuarter = (month = 1) => { ...

Read More

Finding whether a number is triangular number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 475 Views

Triangular numbers are sequences of numbers that can form an equilateral triangle. The nth triangular number is the sum of the first n natural numbers, calculated as T(n) = n × (n + 1) / 2. Triangular Numbers Pattern: T(1) = 1 T(2) = 3 ...

Read More

Converting a string to NATO phonetic alphabets in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 903 Views

The NATO phonetic alphabet is a standardized set of code words used to represent letters of the alphabet in radio and telephone communications. This tutorial shows how to convert any string into NATO phonetic alphabet representation using JavaScript. NATO Phonetic Alphabet The 26 code words are: Alfa, Bravo, Charlie, Delta, Echo, Foxtrot, Golf, Hotel, India, Juliett, Kilo, Lima, Mike, November, Oscar, Papa, Quebec, Romeo, Sierra, Tango, Uniform, Victor, Whiskey, X-ray, Yankee, Zulu. Implementation Here's a complete function that converts a string to NATO phonetic alphabet: const str = 'this is simple string'; const ...

Read More
Showing 2531–2540 of 8,010 articles
« Prev 1 252 253 254 255 256 801 Next »
Advertisements