Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Object Oriented Programming Articles
Page 23 of 589
Return the greatest possible product of n numbers from the array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n, as the second argument. Our function should calculate and return the greatest possible product of n numbers from the array. Problem Analysis To find the maximum product of n numbers, we need to consider both positive and negative numbers. Two negative numbers multiplied together give a positive result, so the strategy involves: Sorting the array to identify the largest and smallest values Choosing ...
Read MoreZig-Zag pattern in strings in JavaScript?
We need to write a function that reads a string and converts the odd indexed characters in the string to upperCase and the even ones to lowerCase and returns a new string. Understanding the Pattern A zig-zag pattern alternates between lowercase and uppercase characters based on their index position. Even indices (0, 2, 4...) become lowercase, while odd indices (1, 3, 5...) become uppercase. Example Implementation const text = 'Hello world, it is so nice to be alive.'; const changeCase = (str) => { const newStr = str ...
Read MoreReturning the highest value from an array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers. Our function should iterate through the array and pick the greatest (largest) element from the array and return that element. Using a Custom Loop Function Here's a manual approach that iterates through the array to find the maximum value: const arr = [5, 3, 20, 15, 7]; const findGreatest = (arr = []) => { let greatest = -Infinity; if (!arr?.length) { return null; ...
Read MoreFetching object keys using recursion in JavaScript
When working with nested objects in JavaScript, you often need to search for specific keys at any level of nesting. Recursion provides an elegant solution for traversing these complex data structures. The Problem Consider a nested object structure where we need to find all values for a specific key across all levels: const people = { Ram: { fullName: 'Ram Kumar', details: { age: ...
Read MoreFind first duplicate item in array in linear time JavaScript
We are required to write a JavaScript function that takes in a read only array of n + 1 integers between 1 and n. The function should find one number that repeats in linear time and using at most O(n) space. For example If the input array is − const arr = [3, 4, 1, 4, 1]; Then the output should be − 4 If there are multiple possible answers (like above), we should output any one. If there is no duplicate, we should output -1. Using Set Data ...
Read MoreHow to remove some items from array when there is repetition in JavaScript
In JavaScript, you may need to remove items from an array that appear in multiples of three (triplets). This is useful when you want to keep only the remaining elements after removing complete sets of triplets. Understanding the Problem The goal is to remove triplets (groups of 3 identical elements) from an array and keep only the remaining elements. For example, if an array has 5 occurrences of the number 1, we remove 3 of them (one triplet) and keep 2. Solution Implementation const arr1 = [1, 1, 1, 3, 3, 5]; const arr2 = ...
Read MoreFinding all possible combined (plus and minus) sums of n arguments JavaScript
We need to write a JavaScript function that takes any number of arguments (all numbers) and finds the sum closest to zero from all possible combinations of addition and subtraction. For example, with arguments 1, 2, 3, the possible combinations are: 1 + 2 + 3 = 6 1 - 2 - 3 = -4 1 + 2 - 3 = 0 1 - 2 + 3 = 2 The function should return the sum closest to 0, which in this case is 0. Algorithm Approach We use a Set to store all ...
Read MoreConverting array of Numbers to cumulative sum array in JavaScript
We have an array of numbers like this: const arr = [1, 1, 5, 2, -4, 6, 10]; We are required to write a function that returns a new array, of the same size but with each element being the sum of all elements until that point (cumulative sum). Therefore, the output should look like: const output = [1, 2, 7, 9, 5, 11, 21]; Let's explore different approaches to create a cumulative sum array. Using forEach() Method We can iterate through the array and build the cumulative sum by adding each element ...
Read MoreNumber of letters in the counting JavaScript
We are required to write a JavaScript function that takes in a number, say n. The function should count the letters in the number names from 1 to n. For example − If n = 5; Then the numbers are one, two, three, four, five. And the total number of letters are 19, so the output should be 19. How It Works The algorithm uses arrays to store the letter count for each digit position and handles special cases like teens (11-19) and compound numbers (hundred, thousand). Example const sumUpto = (num = ...
Read MoreGroup by element in array JavaScript
Grouping array elements by a specific property is a common JavaScript task. This tutorial shows how to group an array of objects by their uuid property into separate sub-arrays. Problem Statement Given an array of objects with similar properties, we need to group them by a specific key and return an array of arrays. const arr = [ {"name": "toto", "uuid": 1111}, {"name": "tata", "uuid": 2222}, {"name": "titi", "uuid": 1111} ]; console.log("Original array:", arr); Original array: [ { name: ...
Read More