Object Oriented Programming Articles

Page 147 of 589

Find unique and biggest string values from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 292 Views

In JavaScript, we often need to filter arrays to get unique values and sort them by specific criteria. This article demonstrates how to find the longest unique string values from an array of objects. Problem Statement Given an array of objects with text properties, we need to create a function that returns n objects with the longest unique string values. If fewer than n unique objects exist, return all unique objects. const arr = [ {text: 'use'}, {text: 'secur'}, {text: 'form'}, ...

Read More

Sum all duplicate value in array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 500 Views

We are required to write a JavaScript function that takes in an array of numbers with duplicate entries and sums all the duplicate entries to one index. For example − If the input array is − const input = [1, 3, 1, 3, 5, 7, 5, 4]; Then the output should be − const output = [2, 6, 7, 10, 4]; Using Map to Track and Sum Duplicates The most efficient approach is to use a Map to count occurrences and then multiply each unique value by its count: ...

Read More

Check whether a number is a Fibonacci number or not JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 524 Views

We are required to write a JavaScript function that takes in a number and returns a boolean based on the fact whether or not it comes in the fibonacci series. For example − If the function call is like this − fibonacci(12); fibonacci(89); fibonacci(55); fibonacci(534); Then the output should be − false true true false What is the Fibonacci Series? The Fibonacci series is a sequence where each number is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Read More

Inverting signs in array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 630 Views

We are required to write a JavaScript function that takes in an array of positive as well as negative Numbers and changes the positive numbers to corresponding negative numbers and the negative numbers to corresponding positive numbers in place. Let's write the code for this function — Example Following is the code — const arr = [12, 5, 3, -1, 54, -43, -2, 34, -1, 4, -4]; const changeSign = arr => { arr.forEach((el, ind) => { arr[ind] *= -1; ...

Read More

Finding next n leap years in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 636 Views

We are required to write a function that takes a positive integer n and returns an array of next n leap years. We will break this problem into three parts: Part 1: Finding Current Year via JavaScript The code to find current year via JavaScript will be: // getting the current year from a new instance of Date object const year = new Date().getFullYear(); console.log("Current year:", year); Current year: 2024 Part 2: Checking for Leap Year We will now write a function isLeap() that takes in a number and returns ...

Read More

Recursive multiplication in array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 436 Views

We need to write a JavaScript function that takes a nested array containing numbers, false values, zeros, and strings, then returns the product of all valid numbers. The function should ignore zeros and falsy values while recursively processing nested arrays. Problem Statement Given a nested array with mixed data types, we want to multiply only the truthy numeric values while ignoring zeros, null, undefined, false, and strings. Solution We'll use recursion to traverse nested arrays and multiply only valid numbers: const arr = [1, 5, 2, null, [ 2, 5, ...

Read More

Reverse sum array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 532 Views

We are required to write a function, say reverseSum() that takes in two arrays of Numbers, let's say first and second and returns a new array that contains: Sum of first element of first array and last element of second array as first element, sum of second element of first array and second last element of second array, and so on. When any of the array runs out of element before the other, we simply push all the remaining elements to the array. Syntax const reverseSum = ...

Read More

Solution to the clumsy factorial problem in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 420 Views

Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n. For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + ...

Read More

Find Second most frequent character in array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 657 Views

We are required to write a JavaScript function that takes in an array and returns the element that appears for the second most number of times. Let's say the following is our array: const arr = [1, 34, 4, 3, 2, 1, 4, 6, 4, 6, 5, 3, 6, 6]; The most frequent element is 6 (appears 4 times), but we want the second most frequent element, which is 4 (appears 3 times). Example const arr = [1, 34, 4, 3, 2, 1, 4, 6, 4, 6, 5, 3, 6, 6]; ...

Read More

Reverse index value sum of array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 281 Views

Suppose we have an array of numbers like this: const arr = [3, 6, 7, 3, 1, 4, 4, 3, 6, 7]; This array contains 10 elements, so the index of the last element is 9. We need to write a function that calculates the sum of each element multiplied by its reverse index position. The reverse index means we multiply each element by (array.length - currentIndex - 1). For our example: // Element at index 0: 3 * (10-0-1) = 3 * 9 = 27 // Element at index 1: 6 * ...

Read More
Showing 1461–1470 of 5,881 articles
« Prev 1 145 146 147 148 149 589 Next »
Advertisements