Found 9313 Articles for Object Oriented Programming

JavaScript Return the lowest index at which a value should be inserted into an array once it has been sorted (either in ascending or descending order).

AmitDiwan
Updated on 26-Aug-2020 11:13:33

124 Views

We have to write a function that returns the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted (either in ascending or descending order). The returned value should be a number.For example, Let’s say, we have a function getIndexToInsert() −getIndexToInsert([1, 2, 3, 4], 1.5, ‘asc’) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).Likewise, getIndexToInsert([20, 3, 5], 19, ‘asc’) should return 2 because once the array has been sorted in ascending order it will look like [3, 5, 20] and ... Read More

Check if three consecutive elements in an array is identical in JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:10:32

640 Views

We are required to write a JavaScript function, say checkThree() that takes in an array and returns true if anywhere in the array there exists three consecutive elements that are identical (i.e., have the same value) otherwise it returns false.Therefore, let’s write the code for this function −Exampleconst arr = ["g", "z", "z", "v" ,"b", "b", "b"]; const checkThree = arr => {    const prev = {       element: null,       count: 0    };    for(let i = 0; i < arr.length; i++){       const { count, element } = prev;   ... Read More

What should be the correct Algorithm to Get Array B from Array A counting backwards from the last element in JavaScript?

AmitDiwan
Updated on 26-Aug-2020 11:08:10

64 Views

Consider the following binary array (Array A) −const arr = [1, 0, 1, 1, 1, 1, 0, 1, 1];When this array is passed through the function, say sumRight(), it produces the following output array (Array B) −const output = [1, 0, 4, 3, 2, 1, 0, 2, 1];Understanding the functionElements in array arr can be either 0 or 1. The function counts backward from the last element of array arr, if there are consecutive 1's in the array arr then the corresponding element in the output array will be 1 but for the 2nd consecutive 1 in array arr, it ... Read More

Filter an object based on an array JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:05:15

644 Views

Let’s say. we have an array and an object like this −const arr = ['a', 'd', 'f']; const obj = {    "a": 5,    "b": 8,    "c": 4,    "d": 1,    "e": 9,    "f": 2,    "g": 7 };We are required to write a function that takes in the object and the array and filter away all the object properties that are not an element of the array. So, the output should only contain 3 properties, namely: “a”, “d” and “e”.Let’s write the code for this function −Exampleconst arr = ['a', 'd', 'f']; const obj = ... Read More

Sort array based on another array in JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:03:57

3K+ Views

We are required to write a sorting function that sort an array based on the contents of another array.For example − We have to sort the original array such that the elements present in the below sortOrder array appear right at the start of original array and all other should keep their order −const originalArray = ['Apple', 'Cat', 'Fan', 'Goat', 'Van', 'Zebra']; const sortOrder = ['Zebra', 'Van'];Exampleconst originalArray = ['Apple', 'Cat', 'Fan', 'Goat', 'Van', 'Zebra']; const sortOrder = ['Zebra', 'Van']; const sorter = (a, b) => {    if(sortOrder.includes(a)){       return -1;    };    if(sortOrder.includes(b)){     ... Read More

Return the first duplicate number from an array in JavaScript

AmitDiwan
Updated on 26-Aug-2020 10:59:41

545 Views

We are required to write a function that returns the index of the very first element that appears at least twice in the array. If no element appears more than once, we have to return -1. The condition is that we have to do this in constant space (i.e., without utilizing extra memory).Let’s devise the solution for this problem. We will use a for loop to iterate over the array and use the Array.prototype.lastIndexOf() method to check for duplicates.Exampleconst firstDuplicate = arr => {    for(let i = 0; i < arr.length; i++){       if(arr.lastIndexOf(arr[i]) !== i){   ... Read More

Split one-dimensional array into two-dimensional array JavaScript

AmitDiwan
Updated on 26-Aug-2020 09:50:07

2K+ Views

We are required to write a function that takes in a one-dimensional array as the first argument and a number n as the second argument and we have to make n subarrays inside of the parent array (**if possible) and divide elements into them accordingly.** if the array contains 9 elements and we asked to make 4 subarrays, then dividing 2 elements in each subarray creates 5 subarrays and 3 in each creates 3, so in such cases we have to fallback to nearest lowest level (3 in this case) because our requirement is to distribute equal number of elements ... Read More

JavaScript merge multiple Boolean arrays with the OR || operator

AmitDiwan
Updated on 26-Aug-2020 09:48:03

359 Views

We have an array of arrays of boolean like this −const arr = [[true, false, false], [false, false, false], [false, false, true]];We are required to write a function that merges this array of arrays into a one dimensional array by combining the corresponding elements of each subarray using the OR (||) operator.Let’s write the code for this function. We will be using Array.prototype.reduce() function to achieve this.Exampleconst arr = [[true, false, false], [false, false, false], [false, false, true]]; const orMerge = arr => {    return arr.reduce((acc, val) => {       val.forEach((bool, ind) => acc[ind] = acc[ind] || ... Read More

JavaScript to push value in empty index in array

AmitDiwan
Updated on 26-Aug-2020 09:45:28

1K+ Views

We have an array that contains some empty values inside it like this −const arr = [43, 534534, 645, 64, , 645, 64, , 645, , 645, , 65, , 645, , 64];We are required to write an Array function pushAtEmpty() which takes in an element and pushes it at the first empty index it finds in the array it is used in context of. If there are no empty spaces, the element should be pushed at the last of the array.Let’s write the code for this function. We will first search for the index of empty position and then ... Read More

Check for Power of two in JavaScript

AmitDiwan
Updated on 26-Aug-2020 09:42:28

2K+ Views

We are required to write a function, say isPowerOfTwo() that takes in a positive number and returns a boolean based on the fact whether or not the number is some power of 2.For example −console.log(isPowerOfTwo(3)); //false console.log(isPowerOfTwo(32)); //true console.log(isPowerOfTwo(2048)); //true console.log(isPowerOfTwo(256)); //true console.log(isPowerOfTwo(22)); //falseLet’s write the code for this function, it will be a very straightforward recursive function that keeps recurring until the number stays divisible by 2, if in this process the number gets reduced all the way down to 1, it is a power of 2 otherwise it isn’t. Here is the code −Exampleconst isPowerOfTwo = num => ... Read More

Advertisements