- 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 9318 Articles for Object Oriented Programming
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
254 Views
Suppose, we have an array of Objects like this −const arr = [ { first_name: 'Lazslo', last_name: 'Jamf' }, { first_name: 'Pig', last_name: 'Bodine' }, { first_name: 'Pirate', last_name: 'Prentice' } ];We are required to write a JavaScript function that takes in one such array and sort this array according to the alphabetical value of the last_name key.ExampleFollowing is the code −const arr = [ { first_name: 'Lazslo', last_name: 'Jamf' }, { first_name: 'Pig', last_name: 'Bodine' }, { first_name: 'Pirate', last_name: 'Prentice' } ]; const sortByLastName = arr => { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
2K+ Views
Given a string that consists of only two types of characters: "(" and ")". We are required to write a function that takes in one such string and balances the parentheses by inserting either a "(" or a ")" as many times as necessary.The function should then return the minimum number of insertions made in the string to balance it. For example −If the string is −const str = '()))';Then the output should be 2, because by prepending '((', we can balance the string.ExampleFollowing is the code −const str = '()))'; const balanceParanthesis = str => { let paren ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
160 Views
We are required to write a function that compares two arrays and creates a third array filling that array with all the elements of the second array and filling null for all those elements that are present in the first array but misses out in the second array.For example −If the two arrays are −const arr1 = ['f', 'g', 'h']; const arr2 = ['f', 'h'];Then the output should be −const output = ['f', null, 'h'];ExampleFollowing is the code −const arr1 = ['f', 'g', 'h']; const arr2 = ['f', 'h']; const compareAndFill = (arr1, arr2) => { let offset = ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
534 Views
Suppose, we have three JavaScript arrays of Number like these −const array1 = [10, 23, 53, 74, 33, 56, 6, 0, 43, 45, 11]; const array2 = [52, 46, 27, 28, 4, 11, 53, 6, 75, 75, 22]; const array3 = [26, 18, 10, 12, 31, 12, 5, 8, 44, 34, 65];The length of all the arrays will always be the same.We are required to write a JavaScript function that in any number of such arrays maps the corresponding elements of an existing array according to their rank (i.e., their order in decreasing sense).Therefore, for the above arrays, the output ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
237 Views
We are required to write a JavaScript function that takes in two arguments −An Array, say arr, of literals that may contain some repeating elements.A number, say limit.The function should validate that no element of the array is repeated more than limit number of times. If any element is repeated more than the limit the function should return false, true otherwise.ExampleFollowing is the code −const arr = [4, 6, 7, 4, 2, 5, 7, 7, 4, 4, 3]; const validateElements = (arr, n) => { const counts = arr.reduce((acc, el) => { acc[el] = (acc[el] + ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
190 Views
Suppose, we have a square matrix represented by a 2-D array in JavaScript like this −const arr = [ [1, 3, 5], [3, 5, 7], [2, 4, 2] ];We are required to write a JavaScript function that takes in one such array.The function should return the difference between the sum of elements present at the diagonals of the matrix.Like for the above matrix, the calculations will be −|(1+5+2) - (5+5+2)| |8 - 12| 4ExampleFollowing is the code −const arr = [ [1, 3, 5], [3, 5, 7], [2, 4, 2] ]; const diagonalDiff = ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
114 Views
We are required to write a JavaScript function that takes in two numbers, say m and n, and it returns an array of first n multiples of m.For example −If the numbers are 4 and 6Then the output should be −const output = [4, 8, 12, 16, 20, 24]ExampleFollowing is the code −const num1 = 4; const num2 = 6; const multiples = (num1, num2) => { const res = []; for(let i = num1; i
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
186 Views
A tidy number is a number whose digits are in non-decreasing order.For example −489 is a tidy number 234557 is also a tidy number 34535 is not a tidy numberWe are required to write a JavaScript function that takes in a number and checks whether its a tidy number or not.ExampleFollowing is the code −const num = 234789; const isTidy = (num, last = 10) => { if(num){ if(num % 10 > last){ return false; }; return isTidy(Math.floor(num / 10), (num % 10)); }; return true; }; console.log(isTidy(num));OutputThis will produce the following output on console −true
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
92 Views
Suppose, we have an array of literals like this −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9];We are required to write a JavaScript function that takes in this array and a number, say n, and returns an object representing the count of elements greater than and smaller than n.ExampleFollowing is the code −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9]; const smallerLargerNumbers = (arr, num) => { return arr.reduce((acc, val) => { let { greater, smaller } = acc; if(val > num){ greater++; }; if(val < num){ smaller++; }; return { greater, smaller }; }, { greater: 0, smaller: 0 }); }; console.log(smallerLargerNumbers(arr, 3));OutputThis will produce the following output on console −{ greater: 8, smaller: 1 }