Javascript Articles

Page 148 of 534

Finding the missing number in an arithmetic progression sequence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 477 Views

An arithmetic progression (AP) or arithmetic sequence is a sequence of numbers where the difference between consecutive terms is constant. For example, the sequence 5, 7, 9, 11, 13... has a common difference of 2. When we have an array representing elements of an arithmetic progression with one missing number, we need to find that missing element efficiently. The key is to determine the common difference and locate where the sequence breaks. Problem Example Given an array like: const arr = [7, 13, 19, 31, 37, 43]; The missing number is 25, which ...

Read More

Finding all valid word squares in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 307 Views

What is a Word Square? A word square consists of a set of words written out in a square grid, such that the same words can be read both horizontally and vertically. This means the character at position (i, j) must equal the character at position (j, i). For instance, one valid word square is: H E A R T E M B E R A B U S E R E S I N T R E N D We need to write a JavaScript function that takes an array of words and ...

Read More

Find Smallest Letter Greater Than Target in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 475 Views

Suppose we are given an array of sorted characters letters containing only lowercase letters. And given a target letter target. We are required to write a JavaScript function that takes in the array as the first argument and the letter as the second. The function is supposed to find the smallest element in the list that is larger than the given target. We have to keep in mind that letters also wrap around. For example, if the target is target = 'z' and letters = ['a', 'b'], the answer is 'a'. Problem Example If the input ...

Read More

Counting all possible palindromic subsequence within a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 557 Views

Palindrome Sequence A palindromic subsequence is a sequence that reads the same from front and back. For instance, 'aba', 'madam', 'did' are all valid palindromic sequences. A subsequence can be contiguous or non-contiguous - we can skip characters but maintain their relative order. We need to write a JavaScript function that counts all possible palindromic subsequences within a string. The input string contains only characters 'a', 'b', 'c', and 'd'. Problem Example For the string 'bccb', the palindromic subsequences are: Input: "bccb" Palindromic subsequences: 'b', 'c', 'c', 'b', 'cc', 'bb', 'bcb', 'bccb' Total count: ...

Read More

Can array be divided into n partitions with equal sums in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 288 Views

We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a number, num, as the second argument. The function should determine whether there exists a way of distributing the elements of the array arr into num groups such that all the groups have equal sum. If there exists any such way, our function should return true, false otherwise. For example − If the input array and the number are − const arr = [4, 6, 3, 3, 7, 4, 1]; const num = 4; ...

Read More

Finding n most frequent words from a sentence in JavaScript

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

For the purpose of this question, we define a sentence as a string that contains English alphabets and punctuations and a word is a substring of that sentence joined together by whitespaces. We are required to write a JavaScript function that takes in a sentence string, str, as the first argument and a number, num, as the second argument. The function should first count the frequency of each word in the sentence and then return an array of length num containing num most frequent words placed according to decreasing frequencies. Problem Example If the input sentence and ...

Read More

Is a number sum of two perfect squares in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 341 Views

Perfect Square Numbers A natural number in mathematics is called a perfect square if it can be obtained by multiplying any other natural number by itself. For instance, 9, 16, 81, 289 are all perfect squares because: 9 = 3 × 3 16 = 4 × 4 81 = 9 × 9 289 = 17 × 17 We need to write a JavaScript function that takes a natural number as input and determines whether it can be expressed ...

Read More

Distance of nearest 0 in binary matrix in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 281 Views

A binary matrix is an array of arrays containing only 0 or 1. We are required to write a JavaScript function that takes in a binary matrix as the only argument. Our function should create a new matrix containing the same number of rows and columns, and for each element of the original matrix the resulting matrix should contain that element's nearest distance from 0 in the original matrix. We have to keep in mind that while calculating distance it can move either horizontally or vertically and not diagonally. And it's guaranteed that the matrix contains at least ...

Read More

Reversing strings with a twist in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 275 Views

We are required to write a JavaScript function that takes in a string str as the first argument and an integer num as the second argument. Our function should reverse the first num characters for every 2 * num characters counting from the start of the string. If there are less than num characters left, we have to reverse all of them. If there are less than 2 * num but greater than or equal to num characters, then we have to reverse the first num characters and leave the others as original. Example Input and Output ...

Read More

Preparing encoding and decoding algorithms for shortening URLs in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 917 Views

URL shortening services like bit.ly and TinyURL take long URLs and convert them into shorter, more manageable links. This process involves encoding the original URL into a compact format and later decoding it back when accessed. To implement a basic URL shortening system in JavaScript, we need two main functions: encrypt() → takes the original URL and returns a shortened unique URL decrypt() → takes the shortened URL and converts it back to the original URL Basic Implementation Using Base64 Encoding Here's a simple approach using ...

Read More
Showing 1471–1480 of 5,340 articles
« Prev 1 146 147 148 149 150 534 Next »
Advertisements