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
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
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
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
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
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
We are required to write a JavaScript function that takes in a string str containing just the characters −'(', ')', '{', '}', '[' and ']'Our function should determine if the input string is valid.An input string is valid if −Open brackets must be closed by the same type of brackets.Open brackets must be closed in the correct order.For example −"()" is a valid parenthesis"()[]{}" is a valid parentheses"(]" is an invalid parenthesisExampleThe code for this will be −const str = "()[]{}"; const isValid = (str = '') => { const map=new Map(); map.set('{', '}'); map.set('(', ')'); ... Read More
We are required to write a JavaScript function that takes in an array of strings. The function should find the longest uncommon subsequence among the strings of the array.By longest uncommon subsequence we mean the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.Our function should return the length of this longest uncommon subsequence.For example: If the input array is −const arr = ["aba", "cdc", "eae"];Then the output should be 3.ExampleThe code for this will be −const arr = ["aba", "cdc", "eae"]; const findUncommonLength = (array = []) => { ... Read More
We are required to write a JavaScript function that takes in two arrays of literals. The arrays might contain some identical entries as well.The purpose of our function is to simply find out and return an array of all such elements that exists in the first array but not in the second.ExampleThe code for this will be −const arr1 = ['1', '2', '3', '4/2', '5/4', '6−2']; const arr2 = ['1', '2', '3', '5/4', '4/2', '6−1', '7/2', '8−2']; const differenceBetween = (arr1 = [], arr2 = []) => { const res = []; for(let i = 0; i < ... Read More
We are required to write a JavaScript function that takes in an array of numbers as the first input and a single number as the second input.The function should find and return the index of the number from the array which is closest to the number specified by second argument.ExampleThe code for this will be −const arr = [0, 65, 131, 196, 259, 323, 388, 453, 517]; const target = 425; const findClosest = (arr, target) => { let min; let chosen = 0; for (let i in arr) { min = Math.abs(arr[chosen] − ... Read More