Articles on Trending Technologies

Technical articles with clear explanations and examples

Removing first k characters from string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 226 Views

We are required to write a JavaScript function that takes in a string and a number, say k and returns another string with first k characters removed from the string. For example: If the original string is − const str = "this is a string" and, n = 4 then the output should be − const output = " is a string" Method 1: Using substr() Method The substr() method extracts characters from a string starting at a specified position. const str = 'this ...

Read More

Expanding Numerals in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 185 Views

We are required to write a function that, given a number, say, 123, will output an array − [100, 20, 3] Basically, the function is expected to return an array that contains the place value of all the digits present in the number taken as an argument by the function. Understanding Place Values Each digit in a number has a place value based on its position. For example, in 123: 1 is in the hundreds place (1 × 100 = 100) 2 is in the ...

Read More

Sorting elements of stack using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 330 Views

We are required to write a JavaScript function that takes in an array of integers and sorts it using recursion with stack operations (push and pop methods). The function sorts the array in-place without using built-in sorting methods. How It Works The algorithm uses two recursive functions: sortStack() - Removes elements from the stack and recursively sorts the remaining elements sortedInsert() - Inserts an element in its correct position in an already sorted stack Example Here's the complete implementation: const stack = [-3, 14, 18, -5, 30]; const sortStack = (stack ...

Read More

Digit sum upto a number of digits of a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 513 Views

We are required to write a JavaScript function that takes in two numbers, let's say m and n as arguments. n will always be smaller than or equal to the number of digits present in m. The function should calculate and return the sum of first n digits of m. Problem Statement If the input numbers are: const m = 5465767; const n = 4; Then the output should be: 20 because 5 + 4 + 6 + 5 = 20 Solution Using String Conversion The most ...

Read More

How to display a message when a given number is in the range using JavaScript?

Prince Varshney
Prince Varshney
Updated on 15-Mar-2026 920 Views

In this article, we will check whether a number lies between a range or not and display a message according to the output we get. This feature of JavaScript allows you to put a number validation while creating a form or any other document. Syntax Following is the syntax to check if number is in range and display the message − if (isNaN(number) || number < lower || number > upper) { document.getElementById("output").innerHTML = number + " is not in range"; } else { document.getElementById("output").innerHTML = number + " is ...

Read More

How to align text content into center using JavaScript?

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

We can align text content into the center using JavaScript by manipulating the CSS properties of HTML elements. This is done by selecting the target element and setting the "text-align" CSS property to "center" using JavaScript's style object. Methods to Center Text There are multiple approaches to center text content using JavaScript: Setting the textAlign property to "center" via JavaScript's style object Using margin properties with auto values for horizontal centering Dynamically creating centered elements with predefined styles Method 1: Using textAlign Property The ...

Read More

How to solve JavaScript heap out of memory on prime number?

Rushi Javiya
Rushi Javiya
Updated on 15-Mar-2026 328 Views

The 'heap out of memory' error occurs when JavaScript code exceeds the allocated memory limit. This commonly happens with inefficient algorithms that have high time or space complexity, especially when processing large numbers. When finding prime factors of very large numbers, naive algorithms can quickly exhaust available memory. The key is optimizing the algorithm to reduce both time and space complexity. The Problem: Inefficient Prime Factor Algorithm The following example demonstrates how a poorly optimized algorithm causes memory issues when processing large numbers: Visualizing Memory Error with Large Numbers ...

Read More

Prime numbers within a range in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 761 Views

We are required to write a JavaScript function that takes in two numbers, say, a and b and returns the total number of prime numbers between a and b (including a and b, if they are prime). For example: If a = 21, and b = 38. The prime numbers between them are 23, 29, 31, 37 And their count is 4 Our function should return 4 Understanding Prime Numbers A prime number is a natural number greater than 1 that has ...

Read More

Reverse mapping an object in JavaScript

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

Reverse mapping an object in JavaScript involves swapping the keys and values of an object. This technique is useful when you need to look up keys based on their values. Suppose we have an object like this: const products = { "Pineapple": 38, "Apple": 110, "Pear": 109 }; All the keys are unique in themselves and all the values are unique in themselves. We need to write a function that accepts a value and returns its corresponding key. Method 1: Using Object.keys() with map() ...

Read More

Sort in multi-dimensional arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 812 Views

Sorting multi-dimensional arrays in JavaScript requires handling each subarray while maintaining specific sorting rules. This article demonstrates how to sort elements within subarrays based on custom conditions. Problem Statement Suppose we have the following array of arrays: const arr = [ ["A", "F", "A", "H", "F", "F"], ["F", "A", "A", "F", "F", "H"] ]; We need to write a JavaScript function that sorts all subarrays according to these rules: If the elements are not either "A" or "F", they should maintain their position If the element is either ...

Read More
Showing 16411–16420 of 61,297 articles
Advertisements