Web Development Articles

Page 212 of 801

Greatest number divisible by n within a bound in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 418 Views

Problem We need to write a JavaScript function that takes in a divisor number n and a bound number b. Our function should find the largest integer num, such that: num is divisible by n (the divisor) num is less than or equal to b (the bound) num is greater than 0 Brute Force Approach The straightforward approach is to iterate through all multiples of n and find the largest one within the bound: const n = 14; const b = 400; const biggestDivisible = ...

Read More

How to convert square bracket object keys into nested object in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 835 Views

In JavaScript, objects with square bracket notation in their keys can be converted into properly nested objects. This is useful when dealing with flat data structures that represent nested relationships. Consider an object with square bracket notation: const flatObj = { "object[foo][bar][ya]": 100 }; console.log("Original object:", flatObj); Original object: { 'object[foo][bar][ya]': 100 } We want to convert this into a nested structure where each bracket represents a deeper level of nesting. Understanding the Problem The goal is to transform a key like "object[foo][bar][ya]" into a nested object structure: ...

Read More

Finding the sum of all common elements within arrays using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 348 Views

Problem We are required to write a JavaScript function that takes in three arrays of numbers. Our function should return the sum of all those numbers that are common in all three arrays. Example Following is the code − const arr1 = [4, 4, 5, 8, 3]; const arr2 = [7, 3, 7, 4, 1]; const arr3 = [11, 0, 7, 3, 4]; const sumCommon = (arr1 = [], arr2 = [], arr3 = []) => { let sum = 0; for(let i = 0; i ...

Read More

Filtering array of objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 855 Views

Filtering arrays of objects is a common task in JavaScript. This article shows how to filter an array of objects based on values from another array using the filter() and includes() methods. Problem Statement Suppose we have two arrays - one containing literal values and another containing objects: const source = [1, 2, 3, 4, 5]; const cities = [{ city: 4 }, { city: 6 }, { city: 8 }]; console.log("Source array:", source); console.log("Cities array:", cities); Source array: [1, 2, 3, 4, 5] Cities array: [{ city: 4 }, { city: ...

Read More

Summing cubes of natural numbers within a range in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 220 Views

Problem We are required to write a JavaScript function that takes in a range array of two numbers. Our function should find the sum of all the cubes of the numbers that falls in the specified range. Understanding the Problem Given a range [a, b], we need to calculate a³ + (a+1)³ + (a+2)³ + ... + b³. For example, with range [4, 11], we calculate 4³ + 5³ + 6³ + 7³ + 8³ + 9³ + 10³ + 11³. Solution Using For Loop Here's the implementation that iterates through the range and sums the cubes: const range = [4, 11]; const sumCubes = ([l, h]) => { const findCube = num => num * num * num; let sum = 0; for(let i = l; i

Read More

Find the smallest sum of all indices of unique number pairs summing to a given number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 221 Views

This tutorial shows how to find pairs of numbers in an array that sum to a target value, then return the sum of all indices of these unique pairs. Problem Description Given an array of numbers and a target sum, we need to: Find all pairs of numbers that sum to the target Ensure each number is used only once across all pairs Return the sum of indices of all numbers used in valid pairs Example Walkthrough For array [1, 4, 2, 3, 0, 5] with target sum 7: Valid pairs: 4 ...

Read More

Finding the 1-based index of a character in alphabets using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 248 Views

We are required to write a JavaScript function that takes in a lowercase English alphabet character. Our function should return the character's 1-based index in the alphabets. Problem Given a lowercase English alphabet character, we need to find its position in the alphabet where 'a' = 1, 'b' = 2, 'c' = 3, and so on. Method 1: Using String indexOf() We can create a string containing all alphabets and use indexOf() to find the position: const char = 'j'; const findCharIndex = (char = '') => { const legend ...

Read More

Read by key and parse as JSON in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 319 Views

In JavaScript, you often need to group JSON data by specific keys and transform the structure. This tutorial shows how to read data by key values and reorganize it into a grouped JSON format. Problem Statement Given a JSON array with nested data, we want to group items by a specific key (like "W") and create a new structure where each group becomes a separate object. Starting with this data structure: const arr = [{ "data": [ { "W": 1, "A1": "123" }, ...

Read More

Constructing an array of addition/subtractions relative to first array element in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 195 Views

We are required to write a JavaScript function that takes in an array of positive integers. Our function should map this array to an array of string integers. The array should contain the number we should add/subtract to the first element to achieve the corresponding element. Problem For example, if we have: [4, 3, 6, 2] We need to calculate the difference between each element and the first element (4): 4 - 4 = 0 → "+0" 3 - 4 = -1 → "-1" 6 - 4 = 2 → "+2" 2 ...

Read More

How to make Ulam number sequence in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 194 Views

A mathematician Ulam proposed generating a sequence of numbers from any positive integer n (n>0) as follows: If n is 1, it will stop. if n is even, the next number is n/2. if n is odd, the next number is 3 * n + 1. continue with the process until reaching 1. This sequence is also known as the Collatz conjecture or 3n+1 problem. It's conjectured that all positive integers eventually reach 1 following these rules. Example Sequences Here are some examples for the first few integers: 2→1 3→10→5→16→8→4→2→1 4→2→1 6→3→10→5→16→8→4→2→1 ...

Read More
Showing 2111–2120 of 8,010 articles
« Prev 1 210 211 212 213 214 801 Next »
Advertisements