Found 9312 Articles for Object Oriented Programming

How can I merge properties of two JavaScript objects dynamically?

AmitDiwan
Updated on 24-Oct-2020 12:23:58

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' }

How to get all combinations of some arrays in JavaScript?

AmitDiwan
Updated on 24-Oct-2020 12:22:27

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' ]

Comparing and filling arrays in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:15:24

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

Fetching odd appearance number in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:10:45

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

Greatest number in a dynamically typed array in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:08:49

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

Finding middlemost element(s) in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:07:14

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 ]

Chunking arrays in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:05:57

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

Remove leading zeros in array in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:04:32

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 ]

Push specific elements to last in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:02:58

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

Finding the balance of brackets in JavaScript

AmitDiwan
Updated on 24-Oct-2020 11:58:53

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

Advertisements