We have two arrays of Numbers, and we are required to write a function, let’s say intersection() that computes their intersection and returns an array that contains the intersecting elements in any order. Each element in the result should appear as many times as it shows in both arrays.For example −If input is −arr1 = ['hello', 'world', 'how', 'are', 'you']; arr2 = ['hey', 'world', 'can', 'you', 'rotate'];Then the output should be −Output: ['world', 'you'];ApproachHad the arrays been sorted, we could have used the two pointer approach with initially both pointing to 0 the start of the respective array and we ... Read More
Let’s say, we have an array of numbers like this −const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0];We are required to write a JavaScript function that counts the consecutive groups of non-negative (positives and 0) numbers in the array.Like here we have consecutive non-negatives from index 3 to 3 (only one element, but still a cluster) which forms one group and then from 9 to end of array forms the second group.Therefore, for this array, the function should return 2.ExampleFollowing is the code −const arr = [-1, -2, -1, 0, -1, -2, -1, ... Read More
Let’s say, we are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as sum of two consecutive elements from the original array.For example, if the input array is −const arr = [3, 6, 3, 87, 3, 23, 2, 2, 6, 8];Then the output should be −const output = [9, 90, 26, 4, 14];ExampleFollowing is the code −const arr = [3, 6, 3, 87, 3, 23, 2, 2, 6, 8]; const twiceSum = arr => { const res = []; for(let i = 0; i < arr.length; i += 2){ res.push(arr[i] + (arr[i+1] || 0)); }; return res; }; console.log(twiceSum(arr));OutputThis will produce the following output in console −[ 9, 90, 26, 4, 14 ]
We have an array of literals like this −const arr = [56768, 5465, 5467, 3, 878, 878, 34435, 78799];We are required to write a JavaScript function that takes in this array and a number n and if the corresponding element contains more than or equal to n characters, then the new element should contain only the last n characters otherwise the element should be left as it is.Therefore, if n = 2, for this array, the output should be −const output = [68, 65, 67, 3, 78, 78, 35, 99];ExampleFollowing is the code −const arr = [56768, 5465, 5467, 3, 878, 878, 34435, 78799]; const splitLast = (arr, num) => { return arr.map(el => { if(String(el).length
We are required to write a JavaScript function that takes in an array of numbers like this −const arr = [3, 6, 34, 12, 6, 8, 8, 5, 6, 8];The function should return the difference between the sum of elements present at the odd index and the sum of elements present at even indexExampleFollowing is the code −const arr = [3, 6, 34, 12, 6, 8, 8, 5, 6, 8]; const oddEvenDiff = arr => { let diff = 0; for(let i = 0; i < arr.length; i++){ if(i % 2 === 0){ diff += arr[i]; }else{ diff -= arr[i] }; }; return Math.abs(diff); }; console.log(oddEvenDiff(arr));OutputThis will produce the following output in console −18
We are required to write a JavaScript function that accepts two string and a number n.The function matches the two strings i.e., it checks if the two strings contains the same characters.The function returns true if both the strings contain the same character irrespective of their order or if they contain at most n different characters, else the function should return false.ExampleFollowing is the code −const str = 'some random text'; const str2 = 'some r@ndom text'; const deviationMatching = (first, second, num) => { let count = 0; for(let i = 0; i < first.length; i++){ ... Read More
We are required to write a JavaScript function that takes in a string and a number n as two arguments (the number should be such that it exactly divides the length of string) and we have to return an array of n strings of equal length.For example −If the string is "how" and the number is 2, our output should be −["h", "o", "w"];Here, every substring exactly contains −(length of array/n) charactersAnd every substring is formed by taking corresponding first and last letters of the string alternatively.ExampleFollowing is the code −const str = "how"; const num = 3; const segregate ... Read More
Let’s say, we have an array of Numbers that contains only 0, 1 and we are required to write a JavaScript function that takes in this array and brings all 1s to the start and 0s to the end.For example − If the input array is −const arr = [1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1];Then the output should be −const output = [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0];ExampleFollowing is the code −const arr = [1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1]; const sortBinary = arr => { const copy = []; for(let i = 0; i − arr.length; i++){ if(arr[i] === 0){ copy.push(0); }else{ copy.unshift(1); }; continue; }; return copy; }; console.log(sortBinary(arr));OutputFollowing is the output in the console −[ 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 ]
We can get all descendants of an element with Selenium webdriver. First of all we need to identify the parent element with help of any of the locators like id, class, name, xpath or css. Then we have to identify the descendants with the findElements(By.xpath()) method.We can find the descendants from the parent element, by localizing it with the parent and then passing ( .//*) as a parameter to the findElements(By.xpath())Syntaxelement.findElements(By.xpath(".//*"))Let us identify the tagname of the descendants of ul element in below html code−Exampleimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class DescendantElements{ public ... Read More
Let’s say, we are required to write a JavaScript function that takes in a string and returns a new string with words rearranged according to their increasing length.ExampleFollowing is the code −const str = 'This is a sample string only'; const arrangeByLength = str => { const strArr = str.split(' '); const sorted = strArr.sort((a, b) => { return a.length - b.length; }); return sorted.join(' '); }; console.log(arrangeByLength(str));OutputFollowing is the output in the console −a is This only sample string