Object Oriented Programming Articles

Page 148 of 589

Divide a string into n equal parts - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 696 Views

We are required to write a JavaScript function that takes in a string and a number n (such that n exactly divides the length of string). We need to return an array of string of length n containing n equal parts of the string. Let's write the code for this function − Example const str = 'this is a sample string'; const num = 5; const divideEqual = (str, num) => { const len = str.length / num; const creds = str.split("").reduce((acc, val) => { ...

Read More

Searching objects by value in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 247 Views

When working with complex JavaScript objects, you often need to find all keys that contain a specific value. This is particularly useful for nested objects where the same value might appear at multiple levels. Consider this example object with nested structure: const obj = { "name": "Vivek Sharma", "occupation": "Software Engineer", "age": 23, "contacts": [{ "name": "Mukul Sharma", "occupation": "Software Engineer", ...

Read More

JavaScript array: Find all elements that appear more than n times

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 447 Views

In JavaScript, you can find all elements that appear more than n times in an array using various approaches. This is useful for data analysis, finding duplicates, or filtering frequent items. Problem Overview Given an array of numbers or strings with repeated entries, we need to write a function that takes a positive integer n and returns all elements that appear more than or equal to n times. Using Map() for Frequency Counting The most efficient approach uses a Map to track element frequencies, then filters elements that meet our criteria: const arr = ...

Read More

Find the average of all elements of array except the largest and smallest - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 388 Views

We are required to write a JavaScript function that takes in an array of numbers and returns the average of its elements excluding the smallest and largest numbers. Approach The solution involves finding the minimum and maximum values, calculating the total sum, then computing the average of remaining elements after excluding min and max values. Example const arr = [1, 4, 5, 3, 5, 6, 12, 5, 65, 3, 2, 65, 9]; const findExcludedAverage = arr => { const creds = arr.reduce((acc, val) => { ...

Read More

Find the difference of largest and the smallest number in an array without sorting it in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 413 Views

We have an array of Numbers that are arranged in pure random order. Our job is to write a function that takes in one such array of Numbers and returns the difference of greatest and smallest numbers present in it, but without sorting the array. We will use the Array.prototype.reduce() function to pick the smallest and greatest numbers from the array and later will return their difference. Using Array.reduce() Method The reduce() method allows us to iterate through the array once and track both maximum and minimum values simultaneously: const arr = [23, 65, 67, ...

Read More

Reduce an array to the sum of every nth element - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 329 Views

We are required to write a JavaScript function that takes in an array of numbers and returns the cumulative sum of every number present at the index that is a multiple of n from the array. Let's write the code for this function − Example const arr = [1, 4, 5, 3, 5, 6, 12, 5, 65, 3, 2, 65, 9]; const num = 2; const nthSum = (arr, num) => { let sum = 0; for(let i = 0; i < arr.length; i++){ ...

Read More

Smallest positive value that cannot be represented as sum of subarray JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 210 Views

We have a sorted array of positive integers like this: const arr = [1, 3, 6, 10, 11, 15]; We are required to write a function, say findSmallest() that takes in one such array and returns the smallest positive integer that cannot be represented as the sum of some subarray of this original array. For example, for the array written above, 2 is the smallest positive integer which cannot be reached by summing any subarray of this original array. Algorithm Approach Since the array is sorted, we can achieve the solution to this ...

Read More

Program to append two given strings such that, if the concatenation creates a double character then omit one of the characters - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 238 Views

We are required to write a JavaScript function that takes in two strings and concatenates the second string to the first string. If the last character of the first string and the first character of the second string are the same then we have to omit one of those characters. Let's say the following are our strings in JavaScript − Problem Example const str1 = 'Food'; const str2 = 'dog'; // Expected output: 'Foodog' (last 'd' of 'Food' matches first 'd' of 'dog') Solution Let's write the code for this function − ...

Read More

Remove all characters of first string from second JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 436 Views

When working with strings in JavaScript, you may need to remove all characters from one string that appear in another string. This is useful for filtering, data cleaning, or text processing tasks. Let's say we have two strings with characters in no specific order. We need to write a function that takes these two strings and returns a modified version of the second string with all characters from the first string removed. Example Strings const first = "hello world"; const second = "hey there"; Using Array Methods (Recommended) The most straightforward approach uses ...

Read More

Prime numbers in a range - JavaScript

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

We are required to write a JavaScript function that takes in two numbers, say, a and b and returns the total number of prime numbers between a and b (including a and b, if they are prime). For example − If a = 2, and b = 21, the prime numbers between them are 2, 3, 5, 7, 11, 13, 17, 19 And their count is 8. Our function should return 8. Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 ...

Read More
Showing 1471–1480 of 5,881 articles
« Prev 1 146 147 148 149 150 589 Next »
Advertisements