Articles on Trending Technologies

Technical articles with clear explanations and examples

How does Selenium WebDriver's isDisplayed() method work?

Debomita Bhattacharjee
Debomita Bhattacharjee
Updated on 18-Sep-2020 1K+ Views

We can work with isDisplayed() method in Selenium webdriver. This method checks if a webelement is visible on the page. If it is visible, then the method returns a true value, else it returns false.First of all, we have to identify the element with any of the locators like id, class, name, xpath or css and then apply isDisplayed() method on it.Syntaxboolean s= driver.findElement(By.id("txt-bx")).isDisplayed();Let us check if the element About Careers at Tutorials Point is displayed on the page. Since it is available it shall return a true value.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 ...

Read More

Finding the intersection of arrays of strings - JavaScript

AmitDiwan
AmitDiwan
Updated on 18-Sep-2020 794 Views

We have two arrays of Numbers, and we are required to write a function, let’s say intersection() that computes their intersection and returns an array that contains the intersecting elements in any order. Each element in the result should appear as many times as it shows in both arrays.For example −If input is −arr1 = ['hello', 'world', 'how', 'are', 'you']; arr2 = ['hey', 'world', 'can', 'you', 'rotate'];Then the output should be −Output: ['world', 'you'];ApproachHad the arrays been sorted, we could have used the two pointer approach with initially both pointing to 0 the start of the respective array and we ...

Read More

Counting the clusters of positive numbers - JavaScript Arrays

AmitDiwan
AmitDiwan
Updated on 18-Sep-2020 306 Views

Let’s say, we have an array of numbers like this −const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0];We are required to write a JavaScript function that counts the consecutive groups of non-negative (positives and 0) numbers in the array.Like here we have consecutive non-negatives from index 3 to 3 (only one element, but still a cluster) which forms one group and then from 9 to end of array forms the second group.Therefore, for this array, the function should return 2.ExampleFollowing is the code −const arr = [-1, -2, -1, 0, -1, -2, -1, ...

Read More

Adding two values at a time from an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 18-Sep-2020 2K+ Views

Let’s say, we are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as sum of two consecutive elements from the original array.For example, if the input array is −const arr = [3, 6, 3, 87, 3, 23, 2, 2, 6, 8];Then the output should be −const output = [9, 90, 26, 4, 14];ExampleFollowing is the code −const arr = [3, 6, 3, 87, 3, 23, 2, 2, 6, 8]; const twiceSum = arr => {    const res = [];    for(let i = 0; i < arr.length; i += 2){       res.push(arr[i] + (arr[i+1] || 0));    };    return res; }; console.log(twiceSum(arr));OutputThis will produce the following output in console −[ 9, 90, 26, 4, 14 ]

Read More

Splitting last n digits of each value in the array in JavaScript

AmitDiwan
AmitDiwan
Updated on 18-Sep-2020 146 Views

We have an array of literals like this −const arr = [56768, 5465, 5467, 3, 878, 878, 34435, 78799];We are required to write a JavaScript function that takes in this array and a number n and if the corresponding element contains more than or equal to n characters, then the new element should contain only the last n characters otherwise the element should be left as it is.Therefore, if n = 2, for this array, the output should be −const output = [68, 65, 67, 3, 78, 78, 35, 99];ExampleFollowing is the code −const arr = [56768, 5465, 5467, 3, 878, 878, 34435, 78799]; const splitLast = (arr, num) => {    return arr.map(el => {       if(String(el).length

Read More

Matching strings for similar characters - JavaScript

AmitDiwan
AmitDiwan
Updated on 18-Sep-2020 647 Views

We are required to write a JavaScript function that accepts two string and a number n.The function matches the two strings i.e., it checks if the two strings contains the same characters.The function returns true if both the strings contain the same character irrespective of their order or if they contain at most n different characters, else the function should return false.ExampleFollowing is the code −const str = 'some random text'; const str2 = 'some r@ndom text'; const deviationMatching = (first, second, num) => {    let count = 0;    for(let i = 0; i < first.length; i++){   ...

Read More

How to get all descendants of an element using webdriver?

Debomita Bhattacharjee
Debomita Bhattacharjee
Updated on 18-Sep-2020 2K+ Views

We can get all descendants of an element 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 descendants with the findElements(By.xpath()) method.We can find the descendants from the parent element, by localizing it with the parent and then passing ( .//*) as a parameter to the findElements(By.xpath())Syntaxelement.findElements(By.xpath(".//*"))Let us identify the tagname of the descendants of ul element 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 DescendantElements{    public ...

Read More

Function that parses number embedded in strings - JavaScript

AmitDiwan
AmitDiwan
Updated on 18-Sep-2020 161 Views

Conventionally, we have functions like parseInt() and parseFloat() that takes in a string and converts the number string to Number. But these methods fail when we have numbers embedded at random index inside the string.For example: The following would only return 454, but what we want is 4545453 −parseInt('454ffdg54hg53')So, we are required to write a JavaScript function that takes in such string and returns the corresponding number.ExampleFollowing is the code −const numStr = '454ffdg54hg53'; const parseInteger = numStr => {    let res = 0;    for(let i = 0; i < numStr.length; i++){       if(!+numStr[i]){     ...

Read More

How to access variables declared in a function, from another function using JavaScript?

AmitDiwan
AmitDiwan
Updated on 18-Sep-2020 2K+ Views

We have to write a function, that does some simple task, say adding two numbers or something like that. We are required to demonstrate the way we can access the variables declared inside that function in some other function or globally.ExampleFollowing is the code −const num = 5; const addRandomToNumber = function(num){    // a random number between [0, 10)    const random = Math.floor(Math.random() * 10);    // assigning the random to this object of function    // so that we can access it outside    this.random = random;    this.res = num + random; }; const addRandomInstance = ...

Read More

If ([] == false) is true, why does ([] || true) result in []? - JavaScript

AmitDiwan
AmitDiwan
Updated on 18-Sep-2020 580 Views

If we look closely at the problem statement, the difference between ([] == false) and ([] || true) is the following −In the first case, we are using loose conditional checking, allowing type coercion to take over.While in the second case, we are evaluating [] to its respective Boolean (truthy or falsy) which makes use of the function Boolean() instead of type coercion under the hook.Let's now unveil the conversions that happens behind the scenes in both cases.Case 1 − ([] == false)According to the MDN docs if two data types say x and y are compared using the loose ...

Read More
Showing 45501–45510 of 61,248 articles
Advertisements