Javascript Articles

Page 128 of 534

Get range of months from array based on another array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 460 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 283 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 359 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 913 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 349 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

Group a JavaScript Array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 349 Views

Grouping JavaScript arrays by a specific property is a common requirement when working with complex data structures. In this tutorial, we'll learn how to group array elements by the tableName property and restructure the data. Problem Statement Suppose we have a JavaScript array with objects containing table data: const data = [ { "dataId": "1", "tableName": "table1", "column": "firstHeader", "rows": ...

Read More

Sort array based on min and max date in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 651 Views

When working with arrays of date strings, you often need to find the oldest (minimum) and newest (maximum) dates. JavaScript provides several approaches to accomplish this task efficiently. const arr = [ "2017-01-22 00:21:17.0", "2017-01-27 11:30:23.0", "2017-01-24 15:53:21.0", "2017-01-27 11:34:18.0", "2017-01-26 16:55:48.0", "2017-01-22 11:57:12.0", "2017-01-27 11:35:43.0" ]; We need to write a JavaScript function that takes such an array, finds the oldest and newest date, and returns an object containing both dates. Using Array.reduce() Method The reduce() method provides an ...

Read More

Calculate average from JSON data based on multiple filters JavaScript

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

When working with JSON data, you often need to filter, group, and calculate averages based on multiple criteria. This article demonstrates how to group objects by multiple fields and compute averages while handling edge cases like undefined values. Problem Statement Given an array of supplier objects, we need to: Group objects with the same "SupplierName" and "Category" Sum their points together (ignoring undefined values) Calculate the average points for each group Return the grouped results with totals and averages ...

Read More

Counting prime numbers from 2 upto the number n JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 459 Views

We are required to write a JavaScript function that takes in a number, say n, as the first and the only argument. The function should then return the count of all the prime numbers from 2 up to the number n (exclusive). For example: For n = 10, the output should be: 4 (2, 3, 5, 7) For n = 1, the output should be: 0 Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime ...

Read More

Convert the number or boolean type JSON object from string type to its original in JavaScript

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

Suppose we have a JSON object where numeric and boolean values are stored as strings: const obj = {"name":"sam", "age":"24", "isMarried":"false"}; console.log(obj); { name: 'sam', age: '24', isMarried: 'false' } Here, the age property should be a number and isMarried should be a boolean. Our goal is to write a function that converts these string values back to their correct data types. Method 1: Using parseInt and Boolean Conversion This approach checks each property and converts numeric strings to numbers and boolean strings to booleans: const obj = { ...

Read More
Showing 1271–1280 of 5,340 articles
« Prev 1 126 127 128 129 130 534 Next »
Advertisements