Javascript Articles

Page 108 of 534

Pushing false objects to bottom in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 222 Views

Suppose we have an array of objects like this − const array = [ {key: 'a', value: false}, {key: 'a', value: 100}, {key: 'a', value: null}, {key: 'a', value: 23} ]; We are required to write a JavaScript function that takes in one such array and places all the objects that have falsy values for the "value" property to the bottom and sorts all other objects in decreasing order by the "value" property. Understanding Falsy Values In JavaScript, falsy values include: false, ...

Read More

Non-composite numbers sum in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 201 Views

In JavaScript, you can calculate the sum of all prime numbers in an array by first checking each number for primality, then adding the prime numbers together. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Understanding Prime Numbers Prime numbers are numbers like 2, 3, 5, 7, 11, 13, etc. The number 1 is not considered prime, and 2 is the only even prime number. Creating a Prime Check Function First, we need a helper function to determine if a number is prime: ...

Read More

Summing up digits and finding nearest prime in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 153 Views

We are required to write a JavaScript function that takes in a number, finds the sum of its digits and returns a prime number that is just greater than or equal to the sum. This problem involves two main steps: calculating the digit sum and finding the nearest prime number greater than or equal to that sum. Breaking Down the Solution Our solution requires three functions: digitSum() - calculates the sum of all digits in a number isPrime() - checks if a number is prime nearestPrime() - finds the nearest prime >= digit sum ...

Read More

Taking common elements from many arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 550 Views

We are required to write a JavaScript function that takes in any arbitrary number of arrays and returns an array of elements that are common to all arrays. If there are no common elements, then we should return an empty array. This problem requires finding the intersection of multiple arrays, which means identifying elements that exist in every provided array. Understanding the Problem The intersection of arrays means finding elements that appear in all arrays. For example, if we have arrays [1, 2, 3], [2, 3, 4], and [3, 4, 5], the common element is 3. ...

Read More

Repeated sum of Number's digits in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 390 Views

We are required to write a JavaScript function that recursively sums up the digits of a number until it reduces to a single digit number. We are required to do so without converting the number to String or any other data type. How It Works The algorithm uses two functions: sumDigit(): Recursively extracts and sums all digits of a number sumRepeatedly(): Repeatedly calls sumDigit() until result is a single digit Example Let's implement the solution step by step: const num = 546767643; const sumDigit = (num, sum = 0) => ...

Read More

Reorder an array in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 10K+ Views

Reordering an array means changing the sequence of elements within the array. JavaScript provides several built-in methods to reorder arrays, each serving different purposes. Using sort() for Alphabetical Ordering The sort() method sorts array elements alphabetically in ascending order by default. It modifies the original array. const states = ["Telangana", "Uttar Pradesh", "Karnataka", "Kerala", "TamilNadu"]; document.getElementById("para1").innerHTML = "Original: " + states; states.sort(); ...

Read More

Sorting Array without using sort() in JavaScript

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

Sorting arrays without using the built-in sort() method is a common programming challenge that helps understand sorting algorithms. JavaScript provides several approaches to implement custom sorting logic. Using Array.reduce() for Insertion Sort The reduce() method can implement insertion sort by building a sorted array incrementally: const arr = [4, 56, 5, 3, 34, 37, 89, 57, 98]; const sortWithReduce = arr => { return arr.reduce((acc, val) => { let ind = 0; while(ind < acc.length ...

Read More

Decimal count of a Number in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 14K+ Views

Given there is a number with digits after decimal point and the task is to find the count of digits after the decimal point. Input Output Scenario Let's look into the input output scenario, where there is a floating point number having some digits after decimal point. Input = 45.36346323 Output = 8 As we can see in the above snippet there are 8 digits in the floating point number after the decimal point. To achieve this task, we'll explore three different methods using isInteger(), toString(), and split() methods. Method 1: Using ...

Read More

Converting two arrays into a JSON object in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 5K+ Views

Given two arrays and the task is to convert those two arrays into a JSON object. Where the first array elements are keys of the object and the second array elements are values of the object. Input-Output Scenario Let's look into the scenario of how two arrays will be converted into a JSON object. Consider two arrays are having some elements. Now, we need to convert these two arrays into a JSON object. Array1 = [1, 2, 3, 4]; Array2 = ['A', 'B', 'C', 'D']; Output = {"1":"A", "2":"B", "3":"C", "4":"D"} //JSON object ...

Read More

Using one array to help filter the other in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 148 Views

When working with arrays of objects, you often need to filter one array based on values from another array. This is a common requirement when you have a list of items and want to keep only those that match specific criteria. Problem Statement Given an array of objects and an array of values, we need to filter the objects array to include only those objects whose property matches values in the second array. Sample Data Let's start with these arrays: const main = [ {name: "Karan", age: 34}, {name: "Aayush", ...

Read More
Showing 1071–1080 of 5,340 articles
« Prev 1 106 107 108 109 110 534 Next »
Advertisements