We are required to write a JavaScript function that takes in a string that contains strings joined by whitespaces. Our function should create a new string that has all the words from the original string and the words whose length is a prime number reversed i.e. words with length 2, 3, 5, 7, 100, etc.ExampleFollowing is the code −const str = 'His father is an engineer by profession'; // helper functions const isPrime = n => { if (n===1){ return false; }else if(n === 2){ return true; }else{ ... Read More
We are required to write a JavaScript function that takes in a string representing a temperature either in Celsius or in Fahrenheit.Like this −"23F", "43C", "23F"We are required to write a JavaScript function that takes in this string and converts the temperature from Celsius to Fahrenheit and Fahrenheit to Celsius.ExampleFollowing is the code −const temp1 = '37C'; const temp2 = '100F'; const tempConverter = temp => { const degree = temp[temp.length-1]; let converted; if(degree === "C") { converted = (parseInt(temp) * 9 / 5 + 32).toFixed(2) + "F"; }else { ... Read More
IntroductionTraversing a big collection of data using looping construct such as foreach would require large memory and considerable processing time. With generators it is possible to iterate over a set of data without these overheads. A generator function is similar to a normal function. However, instead of return statement in a function, generator uses yield keyword to be executed repeatedly so that it provides values to be iterated.The yield keyword is the heart of generator mechanism. Even though its use appears to be similar to return, it doesn't stop execution of function. It provides next value for iteration and pauses ... Read More
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
 Data Structure
 Networking
 RDBMS
 Operating System
 Java
 iOS
 HTML
 CSS
 Android
 Python
 C Programming
 C++
 C#
 MongoDB
 MySQL
 Javascript
 PHP