- 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 9316 Articles for Object Oriented Programming
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
789 Views
Suppose, we have an object like this −const products = { "Pineapple":38, "Apple":110, "Pear":109 };All the keys are unique in themselves and all the values are unique in themselves.We are required to write a function that accepts a value and returns its key. Let' say we have created a function findKey().For example, findKey(110) should return "Apple".We will approach this problem by first reverse mapping the values to keys and then simply using object notations to find their values.Therefore, let’s write the code for this function −ExampleThe code for this will be −const products = { "Pineapple":38, ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
81 Views
We are required to write a function that, given a number, say, 123, will output an array −[100, 20, 3]Basically, the function is expected to return an array that contains the place value of all the digits present in the number taken as an argument by the function.We can solve this problem by using a recursive approach.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 123; const placeValue = (num, res = [], factor = 1) => { if(num){ const val = (num % 10) * factor; ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
158 Views
Suppose, we have an array of arrays of numbers like this −const arr = [[1, 45], [1, 34], [1, 49], [2, 34], [4, 78], [2, 67], [4, 65]];Each subarray is bound to contain strictly two elements. We are required to write a function that constructs a new array where all second elements of the subarrays that have similar first value are grouped together.So, for the array above, the output should look like −const output = [ [45, 34, 49], [34, 67], [78, 65] ];We can make use of the Array.prototype.reduce() method that takes help of a Map() ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
68 Views
We are required to write a JavaScript function that creates a multi-dimensional array based on some inputs.It should take in three elements, namely −row - the number of subarrays to be present in the array, col - the number of elements in each subarray, val - the val of each element in the subarrays, For example, if the three inputs are 2, 3, 10Then the output should be −const output = [[10, 10, 10], [10, 10, 10]];Therefore, let’s write the code for this function −ExampleThe code for this will be −const row = 2; const col = 3; const val ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
6K+ Views
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", sub: {} ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
239 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 4 duplicate values we have to remove then all and insert four empty strings at the end.Therefore, let’s write the code for this function −ExampleThe code for this will be −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 ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
79 Views
We have to write a simple function in JavaScript that takes in an array of Numbers (nested to any level) 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, [ 54, 7, 87, 23, 79, 994, 2 ], 54 ], 54, 4, 2 ];Then the output should be −994We will use recursion to find the greatest number in the array, Therefore, let’s ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
144 Views
We have an array of numbers and 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.ExampleThe code for this will be −const arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, 4, 2, 1]; const consecutiveAverage = arr => { return arr.map((el, ind, array) => { const first = (array[ind-1] || ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
336 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 4 duplicate values we have to remove then all and insert four empty strings at the end.Therefore, let’s write the code for this function −ExampleThe code for this will be −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) => { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
157 Views
We have to write a simple function in JavaScript that takes in an array of Numbers (nested to any level) 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, [ 54, 7, 87, 23, 79, 994, 2 ], 54 ], 54, 4, 2 ];Then the output should be −994We will use recursion to find the greatest number in the array.ExampleThe code ... Read More