We are required to write a JavaScript function that takes in an array of arrays of characters as the first argument and a string as the second argument.The function should find out whether there exist characters in the matrix, non-repeating combination of which yields the string provided to the function as the second argument.If there exists such a combination, our function should return true, false otherwise.For example −If the input array and the string are −const arr = [ ['s', 'd', 'k', 'e'], ['j', 'm', 'o', 'w'], ['y', 'n', 'l'] ]; const str = 'don';Then the output ... Read More
We are required to write a JavaScript function that takes in an array of arrays of numbers as the first argument and a number as the second argument. The subarrays contain numbers sorted in an increasing order and no element of a preceding subarray is greater than any element of the succeeding subarray.The function should use the binary search algorithm to search for the element provided as the second argument in the sorted array of arrays.If the element exists the function should return true, false otherwise.For example −If the input array is −const arr = [ [2, 6, 9, ... Read More
We are required to write a JavaScript function that takes in array of integers as the first and the only argument.Our function should find and return that smallest positive integer which is not present in the array.For example −If the input array is −const arr = [4, 2, -1, 0, 3, 9, 1, -5];Then the output should be −const output = 5;because 1, 2, 3, 4 are already present in the array and 5 is the smallest positive integer absent from the array.ExampleFollowing is the code −const arr = [4, 2, -1, 0, 3, 9, 1, -5]; const findSmallestMissing = ... Read More
We are required to write a JavaScript function that takes in an array of numbers as the first argument and a number as the second argument. The function should prepare and return an array of all such triplets (consecutive or nonconsecutive), that add up to the number specified by the second argument.For example −If the input array and the number are −const arr = [4, 2, 0, 1, 2, 6, 8, 3, 2, 5]; const num = 8;Then the output array should be −const output = [ [ 2, 2, 4 ], [ 1, 3, 4 ], [ 0, 2, ... Read More
We are required to write a JavaScript function that takes in two numbers, let’s say m and n as arguments.n will always be smaller than or equal to the number of digits present in m. The function should calculate and return the sum of first n digits of m.For example −If the input numbers are −const m = 5465767; const n = 4;Then the output should be −const output = 20;because 5 + 4 + 6 + 5 = 20ExampleFollowing is the code −const m = 5465767; const n = 4; const digitSumUpto = (m, n) => { if(n ... Read More
We are required to write a JavaScript function that takes in a positive integer as the first and the only argument.The function should find one such smallest prime number which is just greater than the number specified as argument.For example −If the input is −const num = 18;Then the output should be:const output = 19;ExampleFollowing is the code:const num = 18; const justGreaterPrime = (num) => { for (let i = num + 1;; i++) { let isPrime = true; for (let d = 2; d * d
We are required to write a JavaScript function that takes in a sentence as the first and the only argument.A sentence is a special kind of string of characters joined by finite number of whitespaces.The function should rearrange the words of the sentence such that the smallest word (word with least characters) appears first and then followed by bigger ones.For example −If the input string is −const str = 'this is a string';Then the output should be −const output = 'a is this string';ExampleFollowing is the code −const str = 'this is a string'; const arrangeWords = (str = []) ... Read More
We are required to write a JavaScript function that takes in an array of literals as the first and the only argument.The array is likely to contain many repeating values. Our function should sort the array such that the values that are unique or that have the least frequency are placed before the ones that have the most.For example −If the input array is −const arr = [4, 7, 3, 5, 5, 4, 7, 9, 2, 1, 5, 7, 5, 5, 9];Then the output array should be −const output = [ 3, 2, 1, 9, 9, 4, 4, ... Read More
We are required to write a JavaScript function that takes in a string, say str as the first argument and an array of positive integers, say arr of the same length as the second argument.Our function should shuffle the characters in the string such that the character at the ith position moves to arr[i] in the shuffled string.For example −If the input string and the array are −const str = 'example'; const arr = [5, 2, 0, 6, 4, 1, 3];Then the output should be −const output = 'alxepem';ExampleFollowing is the code −const str = 'example'; const arr = [5, ... Read More
We are required to write a JavaScript function that takes in a string as the first argument and a positive integer n as the second argument.The string is likely to contain some repeating characters. The function should find out and return the length of the longest substring from the original string in which all characters appear at least n number of times.For example −If the input string and the number are −const str = 'kdkddj'; const num = 2;Then the output should be −const output = 5;because the desired longest substring is 'kdkdd'ExampleFollowing is the code −const str = 'kdkddj'; ... Read More