Sorting Array with Standard Values in JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:42:44

189 Views

We are required to sort a dynamic JavaScript array. The condition is that we are required to sort it according to the values stored in a particular order in a standard predefined array.Let’s say the following is our dynamic array −const dbArray = ['Apple', 'Banana', 'Mango', 'Apple', 'Mango', 'Mango', 'Apple'];And suppose the standard array against which we have to sort the above array is like −const stdArray = ['Mango', 'Apple', 'Banana', 'Grapes'];So, after sorting the dbArray, my resultant array should look like −const resultArray = ['Mango', 'Mango', 'Mango', 'Apple', 'Apple', 'Apple', 'Banana'];ExampleFollowing is the code −const dbArray = ['Apple', 'Banana', ... Read More

Get Value of Input Box Using Selenium in Python

Debomita Bhattacharjee
Updated on 18-Sep-2020 09:41:33

12K+ Views

We can get the value of an input box with Selenium webdriver. The get_attribute() method is capable of obtaining the value we have entered in an input box. To get the value, we have to pass value as a parameter to the method.First of all, we have to identify the input box with the help of any of the locators like id, class, name, css or xpath. Then we have to type some values inside it with the send_keys() method.Let us consider the below input box where we shall enter some texts - Selenium Python and then fetch the value ... Read More

Finding Letter Distance in Strings using JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:41:32

722 Views

We are required to write a JavaScript function that takes in a string as first argument and two single element strings. The function should return the distance between those single letter stings in the string taken as first argument.For example −If the three strings are −const str = 'Disaster management'; const a = 'i', b = 't';Then the output should be 4 because the distance between 'i' and 't' is 4ExampleFollowing is the code −const str = 'Disaster management'; const a = 'i', b = 't'; const distanceBetween = (str, a, b) => {    const aIndex = str.indexOf(a);   ... Read More

Finding Shared Element Between Two Strings in JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:39:08

593 Views

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

Add Function for Swapping Cases to String Prototype in JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:38:08

145 Views

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

Get Attribute of Element from Selenium

Debomita Bhattacharjee
Updated on 18-Sep-2020 09:36:56

8K+ Views

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

Mapping Letters of a String to an Object of Arrays in JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:36:56

458 Views

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

Determine Full House in Poker using JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:35:54

440 Views

   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

Declaring Sub-Namespaces in PHP

Malhar Lathkar
Updated on 18-Sep-2020 09:35:24

304 Views

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

Get Selected Option Using Selenium WebDriver with Java

Debomita Bhattacharjee
Updated on 18-Sep-2020 09:34:21

17K+ Views

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

Advertisements