- 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 9312 Articles for Object Oriented Programming
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
145 Views
To merger properties of two objects, you can use the concept of {...object1,...object2,... N).ExampleFollowing is the code −var firstObject = { firstName: 'John', lastName: 'Smith' }; var secondObject = { countryName: 'US' }; var mergeObject = {...firstObject, ...secondObject}; console.log(mergeObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo307.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo307.js { firstName: 'John', lastName: 'Smith', countryName: 'US' }
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
267 Views
You can use your own function to get all combinations.ExampleFollowing is the code −function combination(values) { function * combinationRepeat(size, v) { if (size) for (var chr of values) yield * combinationRepeat(size - 1, v + chr); else yield v; } return [...combinationRepeat(values.length, "")]; } var output = combination([4,5]); console.log(output);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo306.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo306.js [ '44', '45', '54', '55' ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
77 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'];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = ['f', 'g', 'h']; const arr2 = ['f', 'h']; const compareAndFill ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
58 Views
Given an array of integers, we are required to write a function that takes this array and finds the one element that appears an odd number of times.There will always be only one integer that appears an odd number of times. We will approach this problem by sorting the array. Once sorted, we can iterate over the array to pick the element that appears for odd number of times.ExampleThe code for this will be −const arr = [20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]; const findOdd = arr => { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
89 Views
We are required to write a JavaScript function that takes in an array that contains some numbers, some strings and some false values. Our function should return the biggest Number from the array.For example: If the input array is −const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii'];Then the output should be 65.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; const pickBiggest = arr => { let max = -Infinity; for(let i = 0; i ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
63 Views
We are required to write a JavaScript function that takes in an array of Numbers. The function should return the middlemost element of the array.For example: If the array is −const arr = [1, 2, 3, 4, 5, 6, 7];Then the output should be 4.ExampleThe code for this will be −const arr = [1, 2, 3, 4, 5, 6, 7]; const middle = function(){ const half = this.length >> 1; const offset = 1 - this.length % 2; return this.slice(half - offset, half + 1); }; Array.prototype.middle = middle; console.log(arr.middle()); console.log([1, 2, 3, 4, 5, 6].middle());OutputThe output in the console will be −[ 4 ] [ 3, 4 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
117 Views
We are required to write a JavaScript function that takes in an array of literals and returns a new array that have elements of the original array chunked into subarrays of length exactly 2. Now if the length of original array is not exactly divisible by 2, then the last subarray should have only one element.For example: If the input array is −const arr = [1, 2, 3, 4, 5, 6, 7];Then the output should be −const output = [[1, 2], [3, 4], [5, 6], [7]]Therefore, let’s write the code for this function −ExampleThe code for this will be −const ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
307 Views
The requirements for this question are simple. We are required to write a JavaScript function that takes in an array of Numbers.If the array contains leading zero, the function should remove the leading zeros in place, otherwise the function should do nothing.For example: If the input array is −const arr = [0, 0, 0, 14, 0, 63, 0];Then the output should be −const output = [14, 0, 63, 0];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [0, 0, 0, 14, 0, 63, 0]; const removeLeadingZero = arr => { while (arr.indexOf(0) === 0) { arr.shift(); }; }; removeLeadingZero(arr); console.log(arr);OutputThe output in the console will be −[ 14, 0, 63, 0 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
84 Views
Suppose we have an array of objects like this −const arr = [ {flag: true, other: 1}, {flag: true, other: 2}, {flag: false, other: 3}, {flag: true, other: 4}, {flag: true, other: 5}, {flag: true, other: 6}, {flag: false, other: 7} ];We are required to write a JavaScript function that takes in one such array and sorts it based on the following conditions −If arr.flag === false, the matching element gets placed first in the array, but only after the previous matching elements.The elements that do not match, remain in the same order ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
275 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.ExampleThe code for this will be −const str = '()))'; const balanceParanthesis = str => { ... Read More