Front End Technology Articles

Page 211 of 652

Summing cubes of natural numbers within a range in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 231 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

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 841 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 1-based index of a character in alphabets using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 257 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

Filtering array of objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 865 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

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 203 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

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 239 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

Generating desired pairs within a range using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 296 Views

We are required to write a JavaScript function that takes in a number n. Our function should generate an array containing the pairs of integers [a, b] that satisfy the following conditions: 0

Read More

Read by key and parse as JSON in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 326 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

Is the digit divisible by the previous digit of the number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 282 Views

Problem We need to write a JavaScript function that takes a number and checks if each digit is divisible by the digit on its left. The function returns an array of booleans, where the first element is always false since there's no digit before the first one. Example Let's check the number 73312 digit by digit: const num = 73312; const divisibleByPrevious = (n = 1) => { const str = n.toString(); const arr = [false]; for(let i = 1; ...

Read More

How to make Ulam number sequence in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 202 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 2101–2110 of 6,519 articles
« Prev 1 209 210 211 212 213 652 Next »
Advertisements