Found 9321 Articles for Object Oriented Programming

How to convert nested array pairs to objects in an array in JavaScript ?

AmitDiwan
Updated on 24-Nov-2020 09:47:23

847 Views

Suppose, we have an array of arrays like this −const arr = [    [       ['firstName', 'Joe'],       ['lastName', 'Blow'],       ['age', 42],       ['role', 'clerk'],       [          ['firstName', 'Mary'],          ['lastName', 'Jenkins'],          ['age', 36],          ['role', 'manager']       ]    ] ];We are required to write a JavaScript function that takes in one such array. The function should construct an array of objects based on this array of arrays.The output array should ... Read More

How to check if an array contains integer values in JavaScript ?

AmitDiwan
Updated on 24-Nov-2020 09:45:17

1K+ Views

We are required to write a JavaScript function that takes in an array of elements.Our function should check whether or not the array contains an integer value.We should return true if it does false otherwise.ExampleThe code for this will be −const arr = ["123", "", "21345", "90"]; const findInteger = (arr = []) => {    const isInteger = num => {       return typeof num === 'number';    };    const el = arr.find(isInteger);    return !!el; }; console.log(findInteger(arr));OutputAnd the output in the console will be −false

Dash separated cartesian product of any number of arrays in JavaScript

AmitDiwan
Updated on 25-Nov-2020 11:35:37

109 Views

We are required to write a JavaScript function that takes in any number of arrays of literals. The function should compute and return an array of cartesian product of all the elements from the arrays separating them with a dash('−').ExampleThe code for this will be −const arr1= [ 'a', 'b', 'c', 'd' ]; const arr2= [ '1', '2', '3' ]; const arr3= [ 'x', 'y', ]; const dotCartesian = (...arrs) => {    const res = arrs.reduce((acc, val) => {       let ret = [];       acc.map(obj => {          val.map(obj_1 => { ... Read More

Split number into 4 random numbers in JavaScript

AmitDiwan
Updated on 24-Nov-2020 07:02:12

730 Views

We are required to write a JavaScript function that takes in a number as the first input and a maximum number as the second input.The function should generate four random numbers, which when summed should equal the number provided to function as the first input and neither of those four numbers should exceed the number given as the second input.For example − If the arguments to the function are −const n = 10; const max = 4;Then, const output = [3, 2, 3, 2];is a valid combination.Note that repetition of numbers is allowed.ExampleThe code for this will be −const total ... Read More

How to build a string with no repeating character n separate list of characters? in JavaScript

AmitDiwan
Updated on 24-Nov-2020 07:00:33

314 Views

Suppose, we n separate array of single characters. We are required to write a JavaScript function that takes in all those arrays.The function should build all such possible strings that −contains exactly one letter from each arraymust not contain any repeating character (as the arrays might contain common elements)For the purpose of this problem, we will consider these three arrays, but we will write our function such that it works well with variable number of arrays −const arr1 = [a, b ,c, d ]; const arr2 = [e, f ,g ,a]; const arr3 = [m, n, o, g, k];ExampleThe code ... Read More

Check if the string is a combination of strings in an array using JavaScript

AmitDiwan
Updated on 24-Nov-2020 06:58:11

116 Views

We are required to write a JavaScript function that takes in an array of strings as the first argument and a string as the second argument.The function should check whether the string specified by second argument can be formed by combining the strings of the array in any possible way.For example − If the input array is −const arr = ["for", "car", "keys", "forth"];And the string is −const str = "forthcarkeys";Then the output should be true, because the string is a combination of elements at 3, 1 and 2 indexes of the array.ExampleThe code for this will be −const arr ... Read More

Subset with maximum sum in JavaScript

AmitDiwan
Updated on 24-Nov-2020 06:55:04

268 Views

We are required to write a JavaScript function that takes in an array of integers. Our function is required find the subset of non−adjacent elements with the maximum sum.And finally, the function should calculate and return the sum of that subset.For example −If the input array is −const arr = [3, 5, 7, 8, 10];Then the output should be 20 because the non−adjacent subset of numbers will be 3, 7 and 10.ExampleThe code for this will be −const arr = [3, 5, 7, 8, 10]; const maxSubsetSum = (arr = []) => {    let min = −Infinity    const ... Read More

Are the strings anagrams in JavaScript

AmitDiwan
Updated on 24-Nov-2020 06:51:07

132 Views

Anagrams −Two strings are said to be anagrams of each other if by rearranging, rephrasing or shuffling the first we can form a string identical to the second.For example −'something' and 'emosghtin' are anagrams of each other.We are required to write a JavaScript function that takes in two string, say str1 and str2 and return true if they are anagrams of each other, false otherwise.ExampleThe code for this will be −const str1 = "something"; const str2 = "emosghtin"; const validAnagram = (str1 = '', str2 = '') => {    let obj1 = {}    let obj2 = {}   ... Read More

Find what numbers were pressed to get the word (opposite of phone number digit problem) in JavaScript

AmitDiwan
Updated on 24-Nov-2020 06:49:38

100 Views

The mapping of the numerals to alphabets in the old keypad type phones used to be like this −const mapping = {    1: [],    2: ['a', 'b', 'c'],    3: ['d', 'e', 'f'],    4: ['g', 'h', 'i'],    5: ['j', 'k', 'l'],    6: ['m', 'n', 'o'],    7: ['p', 'q', 'r', 's'],    8: ['t', 'u', 'v'],    9: ['w', 'x', 'y', 'z'] };We are required to write a JavaScript function that takes in an alphabet string and return the number combination pressed to type that string.For example −If the alphabet string is −const str = ... Read More

Finding product of an array using recursion in JavaScript

AmitDiwan
Updated on 24-Nov-2020 06:48:32

519 Views

We are required to write a JavaScript function that takes in an array of Integers. Our function should do the following two things −Make use of a recursive approach.Calculate the product of all the elements in the array.And finally, it should return the product.For example −If the input array is −const arr = [1, 3, 6, .2, 2, 5];Then the output should be −const output = 36;ExampleThe code for this will be −const arr = [1, 3, 6, .2, 2, 5]; const arrayProduct = ([front, ...end]) => {    if (front === undefined) {       return 1;    };    return front * arrayProduct(end); }; console.log(arrayProduct(arr));OutputAnd the output in the console will be −36

Advertisements