PHP Object Interfaces

Malhar Lathkar
Updated on 18-Sep-2020 11:15:49

710 Views

IntroductionInterface is an important feature of object oriented programming by which it is possible to specify methods to be implemented by a class, without having to define how they should be implemented.PHP supports interface by way if interface keyword. Interface is similar to class but with methods without definition body. Methods in interface must be public. An inherited class that implements these methods must be defined with implements keyword instead of extends keyword, and must provide implementations of all methods in parent interface.SyntaxAll methods from interface must be defined by the implementing class, otherwise PHP parser throws exceptionExample Live DemoOutputThe error ... Read More

Selenium WebDriver isDisplayed() Method Explained

Debomita Bhattacharjee
Updated on 18-Sep-2020 11:14:57

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

Get Current URL in Selenium WebDriver 2 with Python

Debomita Bhattacharjee
Updated on 18-Sep-2020 11:07:46

1K+ Views

We can get the current URL of a page with Selenium webdriver. The method current_url is available which obtains the present page URL and then we can print the result in the console.Syntaxs = driver.current_urlLet us find the URL of the page presently navigated and we shall get https://www.tutorialspoint.com/index.htm as the output. Examplefrom selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/index.htm") #identify current URL with current_url l= driver.current_url print(Current URL is: " + l) driver.close() Output  Read More

Finding the Intersection of Arrays of Strings in JavaScript

AmitDiwan
Updated on 18-Sep-2020 10:02:47

760 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 Clusters of Positive Numbers in JavaScript Arrays

AmitDiwan
Updated on 18-Sep-2020 10:01:40

281 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

Add Two Values at a Time from an Array in JavaScript

AmitDiwan
Updated on 18-Sep-2020 10:00:25

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 ]

Split Last N Digits of Each Value in the Array in JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:58:01

118 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

Odd Even Index Difference in JavaScript

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

783 Views

We are required to write a JavaScript function that takes in an array of numbers like this −const arr = [3, 6, 34, 12, 6, 8, 8, 5, 6, 8];The function should return the difference between the sum of elements present at the odd index and the sum of elements present at even indexExampleFollowing is the code −const arr = [3, 6, 34, 12, 6, 8, 8, 5, 6, 8]; const oddEvenDiff = arr => {    let diff = 0;    for(let i = 0; i < arr.length; i++){       if(i % 2 === 0){          diff += arr[i];       }else{          diff -= arr[i]       };    };    return Math.abs(diff); }; console.log(oddEvenDiff(arr));OutputThis will produce the following output in console −18

Matching Strings for Similar Characters in JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:53:31

595 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

Segregating a String into Substrings in JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:52:22

225 Views

We are required to write a JavaScript function that takes in a string and a number n as two arguments (the number should be such that it exactly divides the length of string) and we have to return an array of n strings of equal length.For example −If the string is "how" and the number is 2, our output should be −["h", "o", "w"];Here, every substring exactly contains −(length of array/n) charactersAnd every substring is formed by taking corresponding first and last letters of the string alternatively.ExampleFollowing is the code −const str = "how"; const num = 3; const segregate ... Read More

Advertisements