Javascript Articles

Page 158 of 534

Finding nth element of an increasing sequence using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 301 Views

Consider an increasing sequence which is defined as follows: The number seq(0) = 1 is the first one in seq. For each x in seq, then y = 2 * x + 1 and z = 3 * x + 1 must be in seq too. There are no other numbers in seq. Therefore, the first few terms of this sequence will be: [1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...] We are required to write a function ...

Read More

Finding sum of remaining numbers to reach target average using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 202 Views

Problem We are required to write a JavaScript function that takes in an array of numbers and a single number. Our function should find that very number which should be pushed to the array so that its average equals the number specified by the second argument. Understanding the Formula To find the number that makes the average equal to the target, we use this mathematical approach: Current sum of array elements New array length will be (current length + 1) Required total sum = target × new array length ...

Read More

Isosceles triangles with nearest perimeter using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 255 Views

Almost Isosceles Triangle An Almost Isosceles Integer Triangle is a triangle where all side lengths are integers and two sides are almost equal, with their absolute difference being exactly 1 unit of length. Problem Statement We need to write a JavaScript function that takes a number representing the desired perimeter of a triangle. The function should find an almost isosceles triangle whose perimeter is nearest to the input perimeter. For example, if the desired perimeter is 500, the almost isosceles triangle with the nearest perimeter will be [167, 166, 167] with perimeter 500. Understanding Almost ...

Read More

Moving all zeroes present in the array to the end in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 599 Views

We are required to write a JavaScript function that takes in an array of literals that might contain some 0s. Our function should tweak the array such that all the zeroes are pushed to the end and all non-zero elements hold their relative positions. Problem Given an array containing zeros and non-zero elements, we need to move all zeros to the end while maintaining the relative order of non-zero elements. Method 1: Using Two-Pass Approach This approach first collects non-zero elements, then adds zeros at the end: const arr = [5, 0, 1, 0, ...

Read More

All ways of balancing n parenthesis in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 201 Views

Problem We are required to write a JavaScript function that takes in a number n. Our function should return an array showing all the ways of balancing n parenthesis. For example, for n = 3, the output will be: ["()()()", "(())()", "()(())", "(()())", "((()))"] Approach We use a recursive backtracking approach where we keep track of how many opening and closing parentheses we can still use. At each step, we can add an opening parenthesis if we have any left, or a closing parenthesis if it would create a valid balance. Example ...

Read More

Finding the missing number between two arrays of literals in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 663 Views

Finding the missing number between two arrays is a common programming problem where one array is a shuffled version of another with exactly one element removed. Problem Statement We need to write a JavaScript function that takes two arrays: arr1 (original) and arr2 (shuffled duplicate with one missing element). The function should identify and return the missing element. Example const arr1 = [6, 1, 3, 6, 8, 2]; const arr2 = [3, 6, 6, 1, 2]; const findMissing = (arr1 = [], arr2 = []) => { const obj = ...

Read More

Largest index difference with an increasing value in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 154 Views

We need to write a JavaScript function that finds the largest difference between two indices j - i where arr[i] ≤ arr[j]. This means we're looking for the maximum distance between two positions where the value at the later position is greater than or equal to the value at the earlier position. Problem Statement Given an array of numbers, find the maximum value of (j - i) such that arr[i] ≤ arr[j] and i < j. Example Let's implement a solution that checks all possible pairs: const arr = [1, 2, 3, 4]; ...

Read More

Longest string consisting of n consecutive strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 451 Views

Problem We are required to write a JavaScript function that takes in an array of strings. Our function should create combinations by combining all possible n consecutive strings in the array and return the longest such string that comes first. Example Following is the code − const arr = ["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"]; const num = 2; function longestConsec(strarr, k) { if (strarr.length == 0 || k > strarr.length || k longStr.length) { ...

Read More

Counting rings in letters using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 254 Views

We need to write a JavaScript function that counts the number of rings (closed loops) present in letters of a string. Different letters contain different numbers of rings based on their visual structure. Understanding Letter Rings Letters with rings are those that contain closed loops in their visual representation: One ring: A, D, O, P, Q, R, a, b, d, e, g, o, p, q Two rings: B (has two closed loops) No rings: All other letters Example Implementation const str = 'some random text string'; function countRings(str) { ...

Read More

Removing letters to make adjacent pairs different using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 211 Views

We are required to write a JavaScript function that takes in a string containing only 'A', 'B' and 'C'. Our function should find the minimum number of characters needed to be removed from the string so that no two adjacent characters are the same. Problem Statement Given a string with characters 'A', 'B', and 'C', we need to remove the minimum number of characters to ensure all adjacent pairs are different. For example, in "AAB", we need to remove one 'A' to get "AB". Algorithm Approach The strategy is to iterate through the string and whenever ...

Read More
Showing 1571–1580 of 5,340 articles
« Prev 1 156 157 158 159 160 534 Next »
Advertisements