AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 344 of 840

Get the number of true/false values in an array using JavaScript?

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

In JavaScript, you can count true/false values in arrays using various methods. Here are several approaches to accomplish this task. Using filter() Method (Recommended) The most efficient approach is using the filter() method to count values based on conditions: let obj = [ { isMarried: true }, { isMarried: false }, { isMarried: true }, { isMarried: true }, { isMarried: false } ]; // Count true values let trueCount = obj.filter(item => item.isMarried === ...

Read More

Finding the greatest and smallest number in a space separated string of numbers using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 355 Views

We are required to write a JavaScript function that takes in a string containing numbers separated by spaces and returns a string with only the greatest and smallest numbers separated by a space. Problem Statement Given a space-separated string of numbers, find the maximum and minimum values and return them as a formatted string. Input: '5 57 23 23 7 2 78 6' Expected Output: '78 2' Because 78 is the greatest and 2 is the smallest number in the string. Using Array.reduce() Method The most efficient approach uses ...

Read More

Function to add up all the natural numbers from 1 to num in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 336 Views

We are required to write a JavaScript function that takes in a number, say num. Then our function should return the sum of all the natural numbers between 1 and num, including 1 and num. For example, if num is − const num = 5; Then the output should be − const output = 15; because, 1+2+3+4+5 = 15 The Mathematical Formula We will use the efficient mathematical formula to solve this problem − Sum of all natural numbers up to n = ...

Read More

Performing shifts within a string in JavaScript

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

In JavaScript, string shifting involves moving characters from one end of a string to the other. This is commonly used in algorithms and data manipulation tasks where you need to rotate string characters based on specific directions and amounts. String shifts can be performed in two directions: Left shift (0): Remove characters from the beginning and append them to the end Right shift (1): Remove characters from the end and prepend them to the beginning Understanding Shift Operations Let's see how individual shifts work: // Left shift by 1: "abc" -> "bca" ...

Read More

Finding the missing number in an arithmetic progression sequence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 487 Views

An arithmetic progression (AP) or arithmetic sequence is a sequence of numbers where the difference between consecutive terms is constant. For example, the sequence 5, 7, 9, 11, 13... has a common difference of 2. When we have an array representing elements of an arithmetic progression with one missing number, we need to find that missing element efficiently. The key is to determine the common difference and locate where the sequence breaks. Problem Example Given an array like: const arr = [7, 13, 19, 31, 37, 43]; The missing number is 25, which ...

Read More

Return indexes of greatest values in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 466 Views

We are required to write a JavaScript function that takes in an array of Numbers. The array may contain more than one greatest element (i.e., repeating greatest element). We are required to write a JavaScript function that takes in one such array and returns all the indices of the greatest element. Using Array.reduce() Method This approach first finds the maximum value using Math.max(), then uses reduce() to collect all indices where the element equals the maximum value. const arr = [10, 5, 4, 10, 5, 10, 6]; const findGreatestIndices = arr => { ...

Read More

Finding confusing number within an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 322 Views

A number in an array is confusing if it becomes another number which is also present in the array after we rotate it by 180 degrees. When rotated, digits transform as follows: 0→0, 1→1, 6→9, 8→8, 9→6. Other digits (2, 3, 4, 5, 7) cannot be rotated to form valid numbers. Understanding Confusing Numbers For a number to be confusing: It must contain only the digits 0, 1, 6, 8, 9 When rotated 180 degrees, it must form a valid number within our range The rotated number must also be present in the array Example ...

Read More

Applying a custom function to each corresponding element of two arrays using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 238 Views

We need to write a JavaScript function that applies a custom callback function to corresponding elements of two arrays, creating a new array with the results. Problem We are required to write a JavaScript function that takes in a callback function (which takes in two arguments and returns a value) as the first argument and two arrays of essentially the same length as the second and third argument. Our function should construct and return a new array whose each corresponding element is the return value of the callback function if corresponding numbers of the input array are ...

Read More

Use array as sort order in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 206 Views

In JavaScript, you can sort an array of objects based on a custom order defined by another array. This is useful when you need to arrange data according to a specific sequence rather than alphabetical or numerical order. Problem Statement Given a reference array that defines the desired order and an array of objects, we need to sort the objects so their property values match the sequence in the reference array. const sort = ["this", "is", "my", "custom", "order"]; const myObjects = [ {"id":1, "content":"is"}, {"id":2, "content":"my"}, ...

Read More

Checking digit sum of smallest number in the array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 276 Views

We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument. The function should first pick the smallest number from the array and then calculate the sum of all the digits of the number. If the digit sum of that number is even, we should return true, false otherwise. Problem Example If the input array is: const arr = [12, 657, 23, 56, 34, 678, 42]; Then the output should be: const output = false; Because the smallest ...

Read More
Showing 3431–3440 of 8,392 articles
« Prev 1 342 343 344 345 346 840 Next »
Advertisements