- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9314 Articles for Object Oriented Programming
572 Views
We are required to write a function which compares how many values match in an array. It should be sequence dependent. That means i.e. the first object in the first array should be compared to equality to the first object in the second array and so on.For example −If the two input arrays are −const arr1 = [4, 7, 4, 3, 3, 3, 7, 6, 5]; const arr2 = [6, 5, 4, 5, 3, 2, 5, 7, 5];Then the output should be 3.We can solve this problem simply by using a for loop and checking values at the corresponding indices ... Read More
308 Views
Let’s say, we have a special kind of string that contains characters in couple, like this −const str = "AABBCCDDEE";We are required to construct an object based on this string which should look like this −const obj = { code: "AA", sub: { code: "BB", sub: { code: "CC", sub: { code: "DD", sub: { code: "EE", ... Read More
351 Views
Suppose, we have two arrays of literals like these −const arr1 = [4, 23, 7, 6, 3, 6, 4, 3, 56, 4]; const arr2 = [4, 56, 23];We are required to write a JavaScript function that takes in these two arrays and filters the first to contain only those elements that are not present in the second array.And then return the filtered array to get the below output −const output = [7, 6, 3, 6, 3];ExampleFollowing is the code −const arr1 = [4, 23, 7, 6, 3, 6, 4, 3, 56, 4]; const arr2 = [4, 56, 23]; const filterArray = (arr1, arr2) => { const filtered = arr1.filter(el => { return arr2.indexOf(el) === -1; }); return filtered; }; console.log(filterArray(arr1, arr2));OutputThis will produce the following output in console −[ 7, 6, 3, 6, 3 ]
128 Views
Suppose, we have an array and an object like these −const arr = [1, 2, 3, 4, 5]; const obj = { group1: ["Ram", "Mohan", "Shyam"], group2: ["Jai", "Dinesh"], };We are required to zip the array and the object so that the values in the array are assigned to the new objects that are keyed with the values in the object.Like this −const output = { group1: { "Ram": 1, "Mohan": 2, "Shyam": 3 }, group2: { "Jai": 4, "Dinesh": ... Read More
82 Views
We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end.For example −If we find four duplicate values, we have to remove then all and insert four empty strings at the end.ExampleFollowing is the code −const arr = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 5, 12, 1, 23, 4, 1]; const deleteAndInsert = arr => { const creds = arr.reduce((acc, val, ind, array) => { let { count, res } = acc; ... Read More
2K+ Views
Let’s say, we have to write a simple function in JavaScript that takes in the following array of Numbers (nested to any level) −const arr = [ 15, 24, [ 29, 85, 56, [ 36, 14, 6, 98, 34, 52 ], 22 ], 87, 60 ];and return the greatest number present in the array.For example, If the input array is −const arr = [ 34, 65, 67, [ 43, 76, 87, 23, 56, 7, ... Read More
86 Views
Let’s say, we have an array of arrays of Numbers like below −const arr = [ [1, 16, 34, 48], [6, 66, 2, 98], [43, 8, 65, 43], [32, 98, 76, 83], [65, 89, 32, 4], ];We are required to write a function that maps over this array of arrays and returns an array that contains the maximum (greatest) element from each subarray.So, for the above array, the output should be −const output = [ 48, 98, 65, 83, 89 ];ExampleFollowing is the code to get the greatest element from each subarray −const arr = [ [1, 16, 34, 48], [6, 66, 2, 98], [43, 8, 65, 43], [32, 98, 76, 83], [65, 89, 32, 4], ]; const constructBig = arr => { return arr.map(sub => { const max = Math.max(...sub); return max; }); }; console.log(constructBig(arr));OutputThis will produce the following output in console −[ 48, 98, 65, 98, 89 ]
227 Views
Let’s say, we have an array of numbers −const arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, 4, 2, 1];We are required to write a function that returns an array with the average of the corresponding element and its predecessor. For the first element, as there are no predecessors, so that very element should be returned.Let’s write the code for this function, we will use the Array.prototype.map() function to solve this problem −Exampleconst arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, 4, 2, 1]; const consecutiveAverage = arr => { return ... Read More
125 Views
Let’s say, 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. We have to do this in constant space (i.e., without utilizing extra memory).Therefore, let's write 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 duplicacy.ExampleFollowing is the code −const arr1 = [0, 1, 1, 2, 3, 4, 4, 5]; const firstRedundant = arr => { for(let ... Read More
129 Views
We are required to write an Array function, let’s say, pushAtFalsy() The function should take in an array and an element. It should insert the element at the first falsy index it finds in the array.If there are no empty spaces, the element should be inserted at the last of the array.We will first search for the index of empty position and then replace the value there with the value we are provided with.ExampleFollowing is the code −const arr = [13, 34, 65, null, 64, false, 65, 14, undefined, 0, , 5, , 6, ,85, ,334]; const pushAtFalsy = function(element){ ... Read More