Articles on Trending Technologies

Technical articles with clear explanations and examples

Zig-Zag pattern in strings in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 808 Views

We need to write a function that reads a string and converts the odd indexed characters in the string to upperCase and the even ones to lowerCase and returns a new string. Understanding the Pattern A zig-zag pattern alternates between lowercase and uppercase characters based on their index position. Even indices (0, 2, 4...) become lowercase, while odd indices (1, 3, 5...) become uppercase. Example Implementation const text = 'Hello world, it is so nice to be alive.'; const changeCase = (str) => { const newStr = str ...

Read More

Removing duplicates and inserting empty strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 405 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. Problem Description If we find 4 duplicate values we have to remove them all and insert four empty strings at the end. This maintains the original array length while eliminating duplicates. How It Works The algorithm uses reduce() to iterate through the array. For each element, it checks if the current index matches the last occurrence using lastIndexOf(). If yes, it's the final occurrence and gets added to the ...

Read More

Length of longest string chain in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 489 Views

A word chain problem involves finding sequences of words where each word can be formed by adding exactly one character to the previous word. This is a classic dynamic programming problem that requires checking predecessor relationships and building chains efficiently. Understanding Word Chains A word1 is a predecessor of word2 if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac" because we can insert 'a' at position 2. A word chain is a sequence of words [word_1, word_2, ..., word_k] where each word ...

Read More

Expanding binomial expression using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 485 Views

We need to write a JavaScript function that expands binomial expressions of the form (ax+b)^n, where a and b are integers, x is a variable, and n is a natural number. The function returns the expanded polynomial as a string. Problem Statement Given an expression like (8a+6)^4, we need to expand it using the binomial theorem. The result should be in the form ax^b+cx^d+ex^f... with coefficients and powers in decreasing order. Understanding the Binomial Theorem The binomial theorem states that (x+y)^n = Σ(C(n, k) * x^(n-k) * y^k) where C(n, k) is the binomial coefficient. ...

Read More

How to set cursor position in content-editable element using JavaScript?

Rushi Javiya
Rushi Javiya
Updated on 15-Mar-2026 12K+ Views

In HTML, content editable div allows users to edit the content of the div element. We need to pass the contenteditable attribute with true Boolean value to the div element to make any div element editable. The content editable div contains the caret cursor by default, and sometimes we may require to set the caret cursor position in the content editable div element to edit the content of the div. However, we can set the caret cursor position anywhere by clicking at a particular place in the content editable div. This tutorial will teach us to use different ...

Read More

JavaScript - Check if value is a percentage?

Disha Verma
Disha Verma
Updated on 15-Mar-2026 2K+ Views

To check the value for percentage, use a regular expression. When dealing with user input or processing data in JavaScript, it's important to make sure a value is a valid percentage. This article shows you how to check if a value is a percentage using JavaScript, highlighting a useful method that involves regular expressions. Let's say the following is our value: var value = "97%"; To check the value for percentage, we are using a regular expression that matches numbers followed by the % symbol. Using Regular Expression The best way to check ...

Read More

Filter array based on another array in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 14K+ Views

In this article, we are going to learn how to filter an array based on another array in JavaScript. An Array in JavaScript is used to store different elements. These elements are stored at contiguous memory locations. By using index numbers, we can access any or each data element present in the array. Index numbers start from 0. Syntax Following is the syntax of the array in JavaScript – const array_name = [item1, item2, ...]; The following is a simple declaration of array in JavaScript: const colors = ['Blue', 'Limegreen', 'Orange', ...

Read More

JavaScript Reverse the order of the bits in a given integer

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 410 Views

We are required to write a JavaScript program that reverses the order of the bits in a given integer. For example − 56 -> 111000 after reverse 7 -> 111 Another example, 234 -> 11101010 after reverse 87 -> 1010111 How It Works The algorithm converts the number to binary, reverses the binary string, and converts back to decimal: Convert number to binary string using toString(2) Split into array, reverse, and join back Parse reversed binary string to decimal using parseInt(reversedBinary, 2) Example const ...

Read More

Get the max n values from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 682 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n, as the second argument. Our function should then pick the n greatest numbers from the array and return a new array consisting of those numbers. Using Sort and Slice The most straightforward approach is to sort the array in descending order and take the first n elements: const arr = [3, 4, 12, 1, 0, 5, 22, 20, 18, 30, 52]; const pickGreatest = (arr = [], num = 1) ...

Read More

Finding the longest Substring with At Least n Repeating Characters in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 563 Views

We are required to write a JavaScript function that takes in a string as the first argument and a positive integer n as the second argument. The string is likely to contain some repeating characters. The function should find out and return the length of the longest substring from the original string in which all characters appear at least n number of times. For example, if the input string is 'kdkddj' and n is 2, the output should be 5 because the desired longest substring is 'kdkdd'. Problem Understanding Given a string and a number n, ...

Read More
Showing 16371–16380 of 61,299 articles
Advertisements