Javascript Articles

Page 84 of 534

Array thirds with equal sums in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 172 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument. Our function should return true if and only if we can partition the array into three non-empty parts with equal sums, false otherwise. Problem Statement Given an array of integers, determine if it can be split into exactly three contiguous subarrays with equal sums. For example, if the input to the function is: const arr = [3, 3, 6, 5, -2, 2, 5, 1, -9, 4]; Then the output should be: ...

Read More

Finding intersection of arrays of intervals in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 572 Views

JavaScript function that takes in two arrays, arr1 and arr2 of intervals which are pairwise disjoint and in sorted order. A closed interval [a, b] (with a

Read More

Finding squares in sorted order in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 304 Views

We are required to write a JavaScript function that takes in an array of integers, arr, sorted in increasing order. Our function is supposed to return an array of the squares of each number, also sorted in increasing order. For example, if the input to the function is − const arr = [-2, -1, 1, 3, 6, 8]; Then the output should be − const output = [1, 1, 4, 9, 36, 64]; The Challenge Simply squaring and sorting would work, but it's inefficient. Since the original array is ...

Read More

Checking for centrally peaked arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 147 Views

We are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument. Our function should check whether the input array is a centrally peaked array or not. If it is a centrally peaked array, we should return true, false otherwise. Conditions for Centrally Peaked Array The conditions for being a centrally peaked array are: arr.length >= 3 There exists some i with 0 < i < arr.length - 1 such that: ...

Read More

Checking for a Doubleton Number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 351 Views

A "doubleton number" is a natural number that contains exactly two distinct digits. For example, 23, 35, 100, and 12121 are doubleton numbers because they each use only two different digits. Numbers like 123 (three digits) and 9980 (three digits: 9, 8, 0) are not doubleton numbers. Problem Statement We need to write a JavaScript function that takes a number and returns true if it's a doubleton number, false otherwise. Solution The approach is to convert the number to a string, track unique digits using an object, and check if exactly two distinct digits exist. ...

Read More

Converting number of corresponding string without using library function in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 391 Views

Converting a number to a string without using built-in functions like String() or toString() requires extracting individual digits and building the result character by character. Problem We need to write a JavaScript function that takes a number and converts it to its corresponding string representation without using the built-in functions String() or toString() or direct string concatenation. Approach The solution extracts digits from right to left using the modulo operator (%) and integer division. Each digit is converted to its character representation and prepended to build the final string. Example const num = ...

Read More

Converting miles per gallon to kilometer per liter in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 617 Views

Converting miles per gallon (MPG) to kilometers per liter (km/L) is a common unit conversion in JavaScript applications dealing with fuel efficiency across different measurement systems. Understanding the Conversion To convert MPG to km/L, we need two conversion factors: 1 mile = 1.609344 kilometers 1 gallon = 4.54609188 liters (Imperial gallon) The conversion formula is: km/L = MPG × (1.609344 ÷ 4.54609188) Example Implementation const num = 25; const converter = (mpg) => { let LITRES_PER_GALLON = 4.54609188; let KILOMETERS_PER_MILE = 1.609344; ...

Read More

Finding sum of all numbers within a range in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 801 Views

Problem We are required to write a JavaScript function that takes in an array that specifies a range. Our function should find and return the sum of all the natural numbers falling in the range including the range numbers. Example Following is the code − const range = [4, 67]; const findSum = ([l, h]) => { let sum = 0; for(let i = l; i { // Formula: Sum = n * (first + last) / 2 // where ...

Read More

Building a lexicographically increasing sequence of first n natural numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 159 Views

We are required to write a JavaScript function that takes in a number n and returns an array containing the first n natural numbers. The condition is that the numbers should be sorted lexicographically, which means all numbers starting with 1 should come before any starting with 2, 3, 4, and so on. Understanding Lexicographical Ordering Lexicographical ordering treats numbers as strings. For example, "10" comes before "2" because "1" comes before "2" alphabetically. This creates the sequence: 1, 10, 11, 12, ..., 19, 2, 20, 21, ..., etc. Example Implementation const num = 24; ...

Read More

Accumulating some value over using a callback function and initial value in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 253 Views

We need to write a JavaScript function that takes an array, a callback function, and an initial value to accumulate values over array iteration, similar to Array.prototype.reduce(). Problem The goal is to create a custom reduce function that processes each array element through a callback function, accumulating a result from an initial value. Understanding Array Reduce The reduce method applies a callback function to each array element, building up a single result value. The callback receives the accumulator (previous result) and current element as parameters. Custom Reduce Implementation const arr = [1, 2, ...

Read More
Showing 831–840 of 5,340 articles
« Prev 1 82 83 84 85 86 534 Next »
Advertisements