Web Development Articles

Page 226 of 801

Implementing Math function and return m^n in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 235 Views

We are required to write a JavaScript function that takes in two numbers say m and n. Then function should calculate and return m^n. For example − For m = 4, n = 3, then power(4, 3) = 4^3 = 4 * 4 * 4 = 64 power(6, 3) = 216 Using Built-in Math.pow() Method JavaScript provides the built-in Math.pow() method for calculating powers: console.log(Math.pow(4, 3)); // 4^3 console.log(Math.pow(6, 3)); // 6^3 console.log(Math.pow(2, -2)); // 2^(-2) = 1/4 64 216 0.25 Custom Implementation Using Recursion ...

Read More

Finding the sub array that has maximum sum JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 579 Views

We are required to write a JavaScript function that takes in an array of numbers. The array can contain both positive and negative numbers. The purpose of our function is to find the subarray (of any length) whose elements when summed give the maximum sum. This problem is known as the Maximum Subarray Problem and is commonly solved using Kadane's Algorithm. For example, if the input array is: const arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]; Then the output should be: 6 Because the subarray [4, ...

Read More

Difference between two strings JavaScript

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

We are given two strings, say s and t. String t is generated by random shuffling string s and then add one more letter at a random position. We are required to write a JavaScript function that takes both these strings and returns the letter that was added to t. For example − If the input strings are − const s = "abcd", t = "abcde"; Then the output should be − const output = "e"; because 'e' is the letter that was added. Method 1: Using XOR ...

Read More

Checking for overlapping times JavaScript

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

Time interval overlap detection is a common programming challenge. We need to determine if any two time intervals in an array share common time periods. Problem Definition Given an array of time intervals with start and end times, we need to check if any two intervals overlap. Two intervals overlap when they have some time in common. const arr = [ { start: '01:00', end: '04:00' }, { start: '05:00', end: '08:00' }, { start: '07:00', end: '11:00' }, { start: '09:30', end: '18:00' ...

Read More

Sorting an array of objects by an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 702 Views

In JavaScript, you can sort an array of objects based on the order defined by another array. This is useful when you need to prioritize objects according to a specific sequence. Problem Statement Given an array of objects and a reference array, we need to sort the objects according to the order specified in the reference array. const orders = [ { status: "pending"}, { status: "received" }, { status: "sent" }, { status: "pending" } ]; const statuses = ...

Read More

Get range of months from array based on another array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 469 Views

Suppose we have two arrays of strings. The first array contains exactly 12 strings, one for each month of the year like this: const year = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']; console.log(year); [ 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec' ] The second array contains exactly two strings, denoting a range of months like this: const monthsRange = ["aug", "oct"]; console.log(monthsRange); [ 'aug', 'oct' ] We need to ...

Read More

Finding the index position of an array inside an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 287 Views

When working with arrays of arrays in JavaScript, you might need to find the index position of a specific sub-array within the main array. This is useful for checking if a particular array exists and determining its location. Suppose we have an array of arrays like this: const arr = [ [1, 0], [0, 1], [0, 0] ]; We need to write a JavaScript function that takes an array of arrays as the first argument and a target array as the second argument. The function should ...

Read More

Grade book challenge JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 366 Views

We are required to write a function that finds the mean of the three scores passed to it and returns the letter value associated with that grade according to the following table. Grade Scale Score Range Grade 90-100 A 80-89 B ...

Read More

Sorting only a part of an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 927 Views

We are required to write a JavaScript function that takes in an array of strings as the first argument and two numbers as second and third argument respectively. The purpose of our function is to sort the array. But we have to sort only that part of the array that falls between the start and end indices specified by second and third argument. Keeping all the other elements unchanged. For example: const arr = ['z', 'b', 'a']; sortBetween(arr, 0, 1); This function should sort the elements at 0 and 1 index only. And the ...

Read More

How to transform two or more spaces in a string in only one space? JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 365 Views

In JavaScript, you can transform multiple consecutive spaces in a string into a single space using regular expressions with the replace() method. This is useful for cleaning up text input or formatting strings consistently. The Regular Expression Approach The most effective method uses the regular expression /\s{2, }/g where: \s matches any whitespace character (spaces, tabs, newlines) {2, } matches two or more consecutive occurrences g is the global flag to replace all instances Method 1: Using replace() with Regular Expression ...

Read More
Showing 2251–2260 of 8,010 articles
« Prev 1 224 225 226 227 228 801 Next »
Advertisements