We are required to write a JavaScript function that contains the following string −const str = 'This string will be used to calculate frequency distribution';We need to return an object that represents the frequency distribution of various elements present in the array.ExampleFollowing is the code −const str = 'This string will be used to calculate frequency distribution'; const frequencyDistribution = str => { const map = {}; for(let i = 0; i < str.length; i++){ map[str[i]] = (map[str[i]] || 0) + 1; }; return map; }; console.log(frequencyDistribution(str));OutputFollowing is the output in the console ... Read More
We can locate child nodes of web elements 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 children with the findElements(By.xpath()) method.We can identify the child nodes from the parent, by localizing it with the parent and then passing ( ./child::*) as a parameter to the findElements(By.xpath())Syntax−parent.findElements(By.xpath("./child::*"))Let us identify the text of the child nodes of ul node 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 ChildNodes{ ... Read More
We are required to write a JavaScript function that takes in two strings. The first string is some mistyped string and the second string is the correct version of this sting. We can assume that the two strings we are getting as argument will always have the same length.We have to return the number of mistakes that exist in the first array.ExampleFollowing is the code −const str1 = 'Tkos am er exakgrg fetwnh'; const str2 = 'This is an example string'; const countMistakes = (mistaken, correct) => { let count = 0; for(let i = 0; i < ... Read More
IntroductionDeclaration of class, function and constants inside a namespace affects its acess, although any other PHP code can be present in it. PHP's namespace keyword is used to declare a new namespace. A file with .php extension must have namespace declaration in very first line after If namespace declaration is not at the top of file, PHP parser throws fatal errorExample Live Demo Hello world ?>OutputAbove code now returns name following errorPHP Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the scriptOnly declare construct can appear before namespace declarationExample
We are required to write a JavaScript function that takes in two arrays of numbers, second being smaller in size than the first.Our function should be a sorted version of the first array (say in increasing order) but put all the elements that are common in both arrays to the front.For example − If the two arrays are −const arr1 = [5, 4, 3, 2, 1]; const arr2 = [2, 3];Then the output should be −const output = [2, 3, 1, 4, 5];ExampleFollowing is the code −const arr1 = [5, 4, 3, 2, 1]; const arr2 = [2, 3]; // ... Read More
Proper FractionA proper fraction is the one that exists in the p/q form (both p and q being natural numbers)Mixed FractionSuppose we divide the numerator of a fraction (say a) with its denominator (say b), to get quotient q and remainder r.The mixed fraction form for fraction (a/b) will be −qrbAnd it is pronounced as "q wholes and r by b”.We are required to write a JavaScript function that takes in an array of exactly two numbers representing a proper fraction and our function should return an array with three numbers representing its mixed formExampleFollowing is the code −const arr ... Read More
IntroductionIn PHP, namespace keyword is used to define a namespace. It is also used as an operator to request access to certain element in current namespace. The __NAMESPACE__ constant returns name of current namespace__NAMESPACE ConstantFrom a named namespace, __NAMESPACE__ returns its name, for global and un-named namespace it returns empty striingExample Live Demo#test1.php OutputAn empty string is returnedname of global namespace :For named namespace, its name is returnedExample Live DemoOutputname of current namespace : myspaceDynamic name construction__NAMESPACE__ is useful for constructing name dynamicallyExample Live Demo
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