PHP Function Arguments

Malhar Lathkar
Updated on 18-Sep-2020 13:38:56

6K+ Views

IntroductionA function in PHP can be defined to accept input from calling environment/script in the form of arguments. These arguments are given as comma separeted list inside the parentheses in front of name of function. Note that while calling a function, same number of arguments must be passed to it.PHP supports calling a function by passing value, reference, arguments with default value and by passing variable number of arguments.function with argumentsIn following example, a function is defined with two formal arguments. When this function is called by passing the arguments by value. Arguments of function become its local variables. Hence, ... Read More

Find Radio Button Element by Value Using Selenium

Debomita Bhattacharjee
Updated on 18-Sep-2020 13:37:46

3K+ Views

We can find a radio button element by value with Selenium webdriver. A radio button in an html document has a tagname input and it has a type attribute set to radio. We can select a radio button by using the click() method after identifying it.Let us see the html code of a radio button. To identify it with a value attribute we have to use the xpath or css locator.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 RadioButnVal{    public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ... Read More

Return Highest Number from Object Properties Value in JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:37:02

796 Views

Suppose, we have an object that contains rating of a property over some criteria like this −const rating = {    "overall": 92,    "atmosphere": 93,    "cleanliness": 94,    "facilities": 89,    "staff": 94,    "security": 92,    "location": 88,    "valueForMoney": 92 }We are required to write a JavaScript function that takes in one such object and returns the key value pair that has the highest value.For example, for this very object, the output should be −const output = {    "staff": 94 };ExampleFollowing is the code −const rating = {    "overall": 92,    "atmosphere": 93,   ... Read More

Find Indexes of Multiple Minimum Value in an Array in JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:35:48

621 Views

Suppose we have an array of numbers like this −const arr = [1, 2, 3, 4, 1, 7, 8, 9, 1];Suppose we want to find the index of the smallest element in the array i.e. 1 above.For this, we can simply use −const min = Math.min.apply(Math, arr); const ind = arr.indexOf(min);The above code will successfully set ind to 0, which indeed is correct.But what we want to achieve is that if there are more than one minimum elements in the array, like in the above array (three 1s), then we should return an array containing all the indices of minimum ... Read More

For Loop to Evaluate Factorial in JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:34:47

1K+ Views

We are required to write a simple JavaScript function that takes in a Number, say n and computes its factorial using a for loop and returns the factorial.For example −factorial(5) = 120, factorial(6) = 720Maintain a count and a result variable, keep multiplying the count into result, simultaneously decreasing the count by 1, until it reaches 1And then finally we return the result.ExampleFollowing is the code −const num = 14; const factorial = num => {    let res = 1;    for(let i = num; i > 1; i--){       res *= i;    };    return res; }; console.log(factorial(num));OutputThis will produce the following output in console −87178291200

Normalize Numbers in an Object - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:33:49

644 Views

Suppose, we have an object with strings mapped to numbers like this −const obj = {    num1: 45,    num2: 78,    num3: 234,    num4: 3,    num5: 79,    num6: 23 };We are required to write a JavaScript function that takes in one such object as the first argument and an array of strictly two numbers as the second argument.The second argument basically represents a range −[a, b] (b >= a)Our job is to normalize the object values according to the range.Therefore, the largest value of the object must become b and the smallest must become a. ... Read More

Implement Array Prototype lastIndexOf Function in JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:32:11

303 Views

The lastIndexOf() function in JS returns the index of the very last occurrence of the element, passed into it as an argument, in the array, if it exists. If it does not exist the function returns -1.For example −[3, 5, 3, 6, 6, 7, 4, 3, 2, 1].lastIndexOf(3) would return 7.We are required to write a JavaScript function that have the same utility as the existing lastIndexOf() function.And then we have to override the default lastIndexOf() function with the function we just created. We will simply iterate from the back until we find the element and return its index.If we ... Read More

Sum Similar Numeric Values Within Array of Objects in JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:31:07

923 Views

Suppose, we have an array of objects like this −const arr = [    {"firstName":"John", "value": 89},    {"firstName":"Peter", "value": 151},    {"firstName":"Anna", "value": 200},    {"firstName":"Peter", "value": 22},    {"firstName":"Anna", "value": 60} ];We are required to write a JavaScript function that takes in one such array and combines the value property of all those objects that have similar value for the firstName property.Therefore, for the above array, the output should look like −const output = [    {"firstName":"John", "value": 89},    {"firstName":"Peter", "value": 173},    {"firstName":"Anna", "value": 260} ];For each object, we will recursively find their similar objects(Similar objects ... Read More

PHP Return Statement

Malhar Lathkar
Updated on 18-Sep-2020 13:30:52

3K+ Views

IntroductionThe purpose of return statement in PHP is to return control of program execution back to the environment from which it was called. Upon returning, execution of expression following the one which invooked other function or module.If return statement occurs inside a function, execution of current function is terminated, handing over the control back to the environment from which it was called. The return statement may have an exprssion as optional clause in front of it. In that case, value of the expression is also returned in addition to the control.If encountered in an included script, execution of current scripts ... Read More

Remove Blank and Undefined Elements from JavaScript Array

AmitDiwan
Updated on 18-Sep-2020 13:29:52

738 Views

Suppose we have an array of literals like this −const arr = [4, 6, , 45, 3, 345, , 56, 6];We are required to write a JavaScript function that takes in one such array and remove all the undefined elements from the array in place. We are only required to remove the undefined and empty values and not all the falsy values.Use a for loop to iterate over the array and Array.prototype.splice() to remove undefined elements in place.ExampleFollowing is the code −const arr = [4, 6, , 45, 3, 345, , 56, 6] const eliminateUndefined = arr => {   ... Read More

Advertisements