We are required to write a JavaScript function that takes in two strings that may / may not contain some common elements. The function should return an empty string if no common element exists otherwise a string containing all common elements between two strings.Following are our two strings −const str1 = 'Hey There!!, how are you'; const str2 = 'Can this be a special string';ExampleFollowing is the code −const str1 = 'Hey There!!, how are you'; const str2 = 'Can this be a special string'; const commonString = (str1, str2) => { let res = ''; for(let i ... Read More
In JavaScript, we can write our own custom functions and assign them to the existing standard data types (it is quite similar to writing library methods but in this case the data types are primitive and not user defined. We are required to write a JavaScript String function by the name, let’s say swapCase().This function will return a new string with all uppercase characters swapped for lower case characters, and vice versa. Any non-alphabetic characters should be kept as they are.ExampleFollowing is the code −const str = 'ThIS iS A CraZY StRInG'; String.prototype.swapCase = function(){ let res = ''; ... Read More
We can get the attribute of element in Selenium webdriver. The getAttribute() method is used to obtain the value of an attribute in an html document. In an html code, attribute and its value appear as a key value pair.Some of the commonly known html attributes are disabled, alt, id, href, style, title and src. The value of the attribute that we want to fetch is passed as an argument to the method.Let us consider an html code, for which we shall obtain the src attribute. The value of the src attribute should be /about/images/logo.png.Exampleimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; ... Read More
Given a string, we are required to write a function that creates an object that stores the indexes of each letter in an array. The letters (elements) of the string must be the keys of objectThe indexes should be stored in an array and those arrays are values.For example −If the input string is −const str = 'cannot';Then the output should be −const output = { 'c': [0], 'a': [1], 'n': [2, 3], 'o': [4], 't': [5] };ExampleFollowing is the code −const str = 'cannot'; const mapString = str => { const map = ... Read More
The “full house in poker” is a situation when a player, out of their five cards, has at least three cards identical. We are required to write a JavaScript function that takes in an array of five elements representing a card each and returns true if there's a full house situation, false otherwise.ExampleFollowing is the code −const arr2 = ['K', '2', 'K', 'A', 'J']; const isFullHouse = arr => { const copy = arr.slice(); for(let i = 0; i < arr.length; ){ const el = copy.splice(i, 1)[0]; if(copy.includes(el)){ ... Read More
IntroductionIt is possible to create namespaces inside a namespace. Just as a directory in file system can contain sub directories in a heriarchical structure, sub-namespaces can be arranged in hierarchy. The backslash character \ is used to define the relationship between top level and sub-level namespace,In this example toplevel namespace myspace contains two sub-namespaces space1 and space2. In order to access functions/classes inside a subnamespace, first make it available by use keywordExample Live DemoOutputAbove code shows following outputHello World from space2 Hello World from space2
We can get a selected option in a dropdown in Selenium webdriver. The method getFirstSelectedOption() returns the selected option in the dropdown. Once the option is fetched we can apply getText() method to fetch the text.Let us consider the below dropdown Continents get its selected item−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; import org.openqa.selenium.support.ui.Select public class SelecedItem{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String u =" https://www.tutorialspoint.com/selenium/selenium_automation_practice.htm"driver.get(u); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // identify element ... Read More
IntroductionIn a PHP code, appearance of namespace is resolved subject to following rules −A namespace identifier without namespace separator symbol (/) means it is referring to current namespace. This is an unqualified name.If it contains separator symbol as in myspace\space1, it resolves to a subnamespace space1 under myspace. Such type of naming is relative namespace.Name of fully qualified namespace starts with \ character. For example, \myspace or \myspace\space1.Fully qualified names resolve to absolute namespace. For example \myspace\space1 resolves to myspace\space1 namespaceIf the name occurs in the global namespace, the namespace\ prefix is removed. For example namespace\space1 resolves to space1.However, if it occurs ... Read More
We are required to write a JavaScript function that takes in a string of strings joined by whitespaces. The function should calculate and return the average length of all the words present in the string rounded to two decimal placesExampleFollowing is the code −const str = 'This sentence will be used to calculate average word length'; const averageWordLength = str => { if(!str.includes(' ')){ return str.length; }; const { length: strLen } = str; const { length: numWords } = str.split(' '); const average = (strLen - numWords + 1) / numWords; ... Read More
We can get coordinates or dimensions of elements with Selenium webdriver. Each of the elements have .size and .location properties which give the x, y coordinates and height, width of the element in the form of a dictionary.Syntax −loc = element.locations = element.sizeLet us consider an element for which we shall find coordinates and dimensions−Examplefrom selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/about/about_careers.htm") #identify element l= driver.find_element_by_xpath("//img[@class='tp-logo']") #get x, y coordinates loc = l.location #get height, width s = l.size print(loc) print(s) driver.close()OutputRead More