Found 9313 Articles for Object Oriented Programming

Find the middle element of an array using recursion JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:40:06

344 Views

We are required to write an array function, say findMiddle that returns the middlemost element of the array without accessing its length property and without using any kind of built-in loops. If the array contains an odd number of elements, we return the one, middlemost element, or if the array contains an even number of elements, we return an array of two middlemost elements.So, let’s write the code for this function. As you’ve already guessed, we will be making use of recursion to find these elements. The code for the recursive function will be −Exampleconst arr = [1, 2, 3, ... Read More

Check whether a series of operations yields a given number with JavaScript Recursion

AmitDiwan
Updated on 25-Aug-2020 07:38:07

61 Views

By starting from the number 1 and repeatedly either adding 5 or multiplying by 3, an infinite amount of new numbers can be produced. We are required to write a function that, given a number, tries to find a sequence of such additions and multiplications that produce that number. And returns a boolean based on the fact whether or not there exists any such sequenceFor example, The number 13 could be reached by first multiplying by 3 and then adding 5 twice, so the function should return true for 13. Whereas the number 15 cannot be reached at all, so ... Read More

Chunking an array in JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:32:50

681 Views

We are required to write a function chunk() that takes in an array arr of string / number literals as the first argument and a number n as second argument.We are required to return an array of n subarrays, each of which contains at most arr.length / n elements. And the distribution of elements should be like this −The first element goes in the first subarray, second in second, third in third and so on. Once we have one element in each subarray, we again start with filling the first subarray with its second element. Similarly, when all subarrays have ... Read More

How do I recursively remove consecutive duplicate elements from an array?

AmitDiwan
Updated on 25-Aug-2020 07:15:11

465 Views

Suppose, we have an array of Number literals that contains some consecutive redundant entries like this −const testArr = [1, 1, 2, 2, 3, 3, 1, 1, 1];We are supposed to write a function compress that takes in this array and removes all redundant consecutive entries in place. So that the output looks like this −const output = [1, 2, 3, 1];Let’s write the code for this function, we will be using recursion for this and the code for this will be −Exampleconst testArr = [1, 1, 2, 2, 3, 3, 1, 1, 1]; const compress = (arr, len = ... Read More

Check if some elements of array are equal JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:13:13

304 Views

We have an array of numbers that have got some redundant entries, our job is to write a function that takes in the array and groups all the identical entries into one subarray and returns the new array thus formed.For example −//If the input array is: const arr = [1, 3, 3, 1]; //then the output should be: const output = [[1, 1], [3, 3]];We will use a HashMap to keep a track of the elements already occurred and iterate over the array using a for loop, the code for this will be −Exampleconst arr = [1, 3, 3, 1]; ... Read More

How to find distance between items on array JavaScript?

Nikitasha Shrivastava
Updated on 18-May-2023 14:08:45

1K+ Views

In this problem statement, our aim is to find the distance between items on an array with the help of Javascript. So for doing this task we will use a loop and a variable to store the id for each object in the JSON object. Understanding the problem statement The problem statement is to write a function in Javascript by which we can find the distance between items on an array. If we have an array, the distance will be measured by counting the index numbers from the first item from where we have to measure the distance. For example ... Read More

Flatten an array in JavaScript.

AmitDiwan
Updated on 25-Aug-2020 07:08:53

211 Views

We are required to write a JavaScript array function that takes in a nested array and returns an array with all the elements present in the array without any nesting.For example −//if the input is: const arr = [[1, 2, 3], [4, 5], [6]]; //then the output should be: const output = [1, 2, 3, 4, 5, 6];Therefore, let’s write the code for this function −Method 1: Using recursionHere we will loop over the original nested array and recursively push the nested element elements into a new array.Exampleconst arr = [[1, 2, 3], [4, 5], [6]]; const flatten = function(){ ... Read More

Get minimum number without a Math function JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:06:36

199 Views

We have to write a function that takes in n Number literals as argument, where n is any whole number and returns the smallest number of those numbers without using any library function.We will solve this problem via a while loop and the code for this will be −Exampleconst numbers = [12, 5, 7, 43, -32, -323, 5, 6, 7, 767, 23, 7]; const findMin = (...numbers) => {    let min = Infinity, len = 0;    while(len++ < numbers.length){       min = numbers[len] < min ? numbers[len] : min;    }    return min; }; console.log(findMin(...numbers));OutputThe output in the console will be −-323

Counting how many times an item appears in a multidimensional array in JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:05:36

779 Views

We have a nested array of strings and we have to write a function that accepts the array and a search string and returns the count of the number of times that string appears in the nested array.Therefore, let’s write the code for this, we will use recursion here to search inside of the nested array and the code for this will be −Exampleconst arr = [    "apple",    ["banana", "strawberry", "dsffsd", "apple"],    "banana",    ["sdfdsf", "apple", ["apple", ["nonapple", "apple", ["apple"]]]]    , "apple"];    const calculateCount = (arr, query) => {       let count = ... Read More

How can I split an array of Numbers to individual digits in JavaScript?

AmitDiwan
Updated on 25-Aug-2020 07:03:37

431 Views

We have an array of Number literals, and we are required to write a function, say splitDigit() that takes in this array and returns an array of Numbers where the numbers greater than 10 are splitted into single digits.For example −//if the input is: const arr = [ 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106 ] //then the output should be: const output = [ 9, 4, 9, 5, 9, 6, 9, 7, 9, 8, 9, 9, 1, 0, 0, 1, 0, 1, 1, 0, 2, 1, 0, 3, 1, 0, 4, 1, 0, ... Read More

Advertisements