We are required to write a JavaScript function that takes in two strings, creates and returns a new string with first two words of first string, next two words of second string, then first, then second and so on.For example −If the strings are −const str1 = 'Hello world'; const str2 = 'How are you btw';Then the output should be −const output = 'HeHollw o arwoe rlyodu btw';ExampleLet us write the code for this function −const str1 = 'Hello world'; const str2 = 'How are you btw'; const twiceJoin = (str1 = '', str2 = '') => { let ... Read More
We can find parent elements with Selenium webdriver. First of all we need to identify the child element with help of any of the locators like id, class, name, xpath or css. Then we have to identify the parent element with the find_element_by_xpath() method.We can identify the parent from the child, by localizing it with the child and then passing (..) as a parameter to the find_element_by_xpath().Syntax−child.find_element_by_xpath("..")Let us identify class attribute of parent ul from the child element li in below html code−The child element with class heading should be able to get the parent element having toc chapters class ... Read More
We are required to write a JavaScript function that takes in a string and returns the character which makes second most appearances in the string.ExampleFollowing is the code −const str = 'Hello world, I have never seen such a beautiful weather in the world'; const secondFrequent = str => { const map = {}; for(let i = 0; i < str.length; i++){ map[str[i]] = (map[str[i]] || 0) + 1; }; const freqArr = Object.keys(map).map(el => [el, map[el]]); freqArr.sort((a, b) => b[1] - a[1]); return freqArr[1][0]; }; console.log(secondFrequent(str));OutputFollowing is the output in the console −e
IntroductionWhen a generator function is called, internally, a new object of Generator class is returned. It implements the Iterator interface. The iterator interface defines following abstract methodsIterator::current — Return the current elementIterator::key — Return the key of the current elementIterator::next — Move forward to next elementIterator::rewind — Rewind the Iterator to the first elementIterator::valid — Checks if current position is validGenerator acts as a forward-only iterator object would, and provides methods that can be called to manipulate the state of the generator, including sending values to and returning values from it.Generator as interatorIn following example, generator functions yields lines in a file ... Read More
We are required to write a JavaScript function that takes in a string and starts mapping its characters from 0. And every time the function encounters a unique (non-duplicate) character, it should increase the mapping count by 1 otherwise map the same number for duplicate characters.For example − If the string is −const str = 'heeeyyyy';Then the output should be −const output = [0, 1, 1, 1, 2, 2, 2, 2];ExampleFollowing is the code −const str = 'heeeyyyy'; const mapString = str => { const res = []; let curr = '', count = -1; for(let i ... Read More
We are required to write a JavaScript function that takes in a number and the function establishes if the provided number is a semiprime or not.SemiprimeA semiprime number is that number which is a special type of composite number that is a product of two prime numbers. For example: 6, 15, 10, 77 are all semiprime. The square of a prime number is also semiprime, like 4, 9, 25 etc.ExampleFollowing is the code to check semi-prime numbers −const num = 141; const checkSemiprime = num => { let cnt = 0; for (let i = 2; cnt < ... Read More
IntroductionThrowable interface is implemented by Error and Exception class. All predefined Error classes are inherited from Error class. Instance of corresponding Error class is thrown inside try block and processed inside appropriate catch block.Throwing ErrorNormal execution (when no exception is thrown within the try block) will continue after that last catch block defined in sequence.Example Live DemoOutputFollowing output is displayed2 Caught exception: Division by zero. Execution continuesIn following example, TypeError is thrown while executing a function because appropriate arguments are not passed to it. Corresponding error message is displayedExample Live DemoOutputFollowing output is displayedArgument 2 passed to add() must be of the ... Read More
We are required to write a JavaScript function that takes in an array of numbers and reorders the digit of all the numbers internally in a specific order (lets say in ascending order for the sake of this problem).For example − If the array is −const arr = [543, 65, 343, 75, 567, 878, 87];Then the output should be −const output = [345, 56, 334, 57, 567, 788, 78];ExampleFollowing is the code −const arr = [543, 65, 343, 75, 567, 878, 87]; const ascendNumber = num => { const numArr = String(num).split('').map(el => +el); numArr.sort((a, b) => a ... Read More
We can get the current contents of an element in Selenium webdriver. For the elements having tagname as we have to use the getAttribute() method and pass the value parameter as an argument to that method to obtain the current contents.For the elements without an input tag we have to use the getText() method to obtain the current contents. First of all we have to identify the element with the help of the locators.Let us try to get the content Selenium inside the edit box and its above text content – You are browsing the best resource for Online ... Read More
We are required to write a JavaScript function that takes in a string and returns the count of redundant characters in the string.For example − If the string is −const str = 'abcde'Then the output should be 0If the string is −const str = 'aaacbfsc';Then the output should be 3ExampleFollowing is the code −const str = 'aaacbfsc'; const countRedundant = str => { let count = 0; for(let i = 0; i < str.length; i++){ if(i === str.lastIndexOf(str[i])){ continue; }; count++; }; return count; }; console.log(countRedundant(str));OutputFollowing is the output in the console −3