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
Javascript Articles
Page 156 of 534
Validating push pop sequence in JavaScript
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 MoreMaking array unique in JavaScript
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 MoreFinding the nth element of the lucas number sequence in JavaScript
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 MoreConverting to hex and summing the numeral part in JavaScript
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 MoreFinding quarter based on month index in JavaScript
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 MoreFinding whether a number is triangular number in JavaScript
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 MoreConverting a string to NATO phonetic alphabets in JavaScript
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 MoreFinding number plate based on registration number in JavaScript
The car registering system of a city assigns unique identifiers to customers and corresponding number plates to their vehicles. We need to create a JavaScript function that converts a customer ID into its corresponding number plate format. Problem Statement The car registration system assigns two types of identifiers: Customer ID − A natural number between 0 and 17, 558, 423 (inclusive), assigned sequentially starting from 0 Number Plate − Contains a series ...
Read MoreSwapping string case using a binary number in JavaScript
We need to write a JavaScript function that takes a string and a number, then uses the binary representation of the number to determine which alphabetic characters should have their case swapped. Problem Each bit in the binary representation of the number specifies whether to swap the case for each alphabetic character: If the bit is 1, swap the case (lowercase → uppercase, uppercase → lowercase) If the bit is 0, leave the character as is When we reach the end of the binary representation, start again from the first bit Non-alphabetic characters remain unchanged ...
Read MoreBuilding an array of specific size with consecutive element sum being perfect square in JavaScript
We need to create a JavaScript function that arranges numbers 1 to n such that the sum of each pair of consecutive elements forms a perfect square. This is a classic backtracking problem that requires careful arrangement of numbers. Problem Understanding For an array of size n, we must arrange integers 1 through n where each adjacent pair sums to a perfect square. For example, if two consecutive numbers are 9 and 7, their sum (16) should be a perfect square (4²). Algorithm Approach We use a backtracking algorithm with these steps: Try each ...
Read More