Resolving or Rejecting Promises in JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:25:16

113 Views

We are required to write a JavaScript function that imitates a network request, for that we can use the JavaScript setTimeout() function, that executes a task after a given time interval.Our function should return a promise that resolves when the request takes place successfully, otherwise it rejectsExampleFollowing is the code −const num1 = 45, num2 = 48; const res = 93; const expectedSumToBe = (num1, num2, res) => {    return new Promise((resolve, reject) => {       setTimeout(() => {          if(num1 + num2 === res){             resolve('success');     ... Read More

Accessing Global Classes in PHP

Malhar Lathkar
Updated on 18-Sep-2020 09:25:03

371 Views

IntroductionWhen PHP parser encounters an unqulified identifier such as class or function name, it resolves to current namespace. Therefore, to access PHP's predefined classes, they must be referred to by their fully qualified name by prefixing \.Using built-in classIn following example, a new class uses predefined stdClass as base class. We refer it by prefixing \ to specify global classExampleIncluded files will default to the global namespace. Hence, to refer to a class from included file, it must be prefixed with \Example#test1.php This file is included in another PHP script and its class is referred with \when this file is included ... Read More

PHP Global Space

Malhar Lathkar
Updated on 18-Sep-2020 09:23:08

277 Views

IntroductionIn absence of any namespace definition, all definitions of class, function etc. are placed in a global namespace. If a name is prefixed with \ , it will mean that the name is required from the global space even in the context of the namespace.Using global space specificationExampleIncluded files will default to the global namespace.Example#test1.php this will print empty stringwhen this file is included in another namespaceExample#test2.php OutputThis will print following outputtestspace

Frequency Distribution of Elements in JavaScript

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

1K+ Views

We are required to write a JavaScript function that contains the following string −const str = 'This string will be used to calculate frequency distribution';We need to return an object that represents the frequency distribution of various elements present in the array.ExampleFollowing is the code −const str = 'This string will be used to calculate frequency distribution'; const frequencyDistribution = str => {    const map = {};    for(let i = 0; i < str.length; i++){       map[str[i]] = (map[str[i]] || 0) + 1;    };    return map; }; console.log(frequencyDistribution(str));OutputFollowing is the output in the console ... Read More

Locating Child Nodes of WebElements in Selenium

Debomita Bhattacharjee
Updated on 18-Sep-2020 09:22:12

21K+ Views

We can locate child nodes of web elements 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 children with the findElements(By.xpath()) method.We can identify the child nodes from the parent, by localizing it with the parent and then passing ( ./child::*) as a parameter to the findElements(By.xpath())Syntax−parent.findElements(By.xpath("./child::*"))Let us identify the text of the child nodes of ul node 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 ChildNodes{   ... Read More

Finding Mistakes in a String using JavaScript

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

491 Views

We are required to write a JavaScript function that takes in two strings. The first string is some mistyped string and the second string is the correct version of this sting. We can assume that the two strings we are getting as argument will always have the same length.We have to return the number of mistakes that exist in the first array.ExampleFollowing is the code −const str1 = 'Tkos am er exakgrg fetwnh'; const str2 = 'This is an example string'; const countMistakes = (mistaken, correct) => {    let count = 0;    for(let i = 0; i < ... Read More

Defining Namespace in PHP

Malhar Lathkar
Updated on 18-Sep-2020 09:21:16

148 Views

IntroductionDeclaration of class, function and constants inside a namespace affects its acess, although any other PHP code can be present in it. PHP's namespace keyword is used to declare a new namespace. A file with .php extension must have namespace declaration in very first line after If namespace declaration is not at the top of file, PHP parser throws fatal errorExample Live Demo Hello world ?>OutputAbove code now returns name following errorPHP Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the scriptOnly declare construct can appear before namespace declarationExample

Implementing Priority Sort in JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:20:26

2K+ Views

We are required to write a JavaScript function that takes in two arrays of numbers, second being smaller in size than the first.Our function should be a sorted version of the first array (say in increasing order) but put all the elements that are common in both arrays to the front.For example − If the two arrays are −const arr1 = [5, 4, 3, 2, 1]; const arr2 = [2, 3];Then the output should be −const output = [2, 3, 1, 4, 5];ExampleFollowing is the code −const arr1 = [5, 4, 3, 2, 1]; const arr2 = [2, 3]; // ... Read More

Convert Proper Fraction to Mixed Fraction in JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:19:27

493 Views

Proper FractionA proper fraction is the one that exists in the p/q form (both p and q being natural numbers)Mixed FractionSuppose we divide the numerator of a fraction (say a) with its denominator (say b), to get quotient q and remainder r.The mixed fraction form for fraction (a/b) will be −qrbAnd it is pronounced as "q wholes and r by b”.We are required to write a JavaScript function that takes in an array of exactly two numbers representing a proper fraction and our function should return an array with three numbers representing its mixed formExampleFollowing is the code −const arr ... Read More

PHP Namespace Keyword and Namespace Constant

Malhar Lathkar
Updated on 18-Sep-2020 09:19:17

446 Views

IntroductionIn PHP, namespace keyword is used to define a namespace. It is also used as an operator to request access to certain element in current namespace. The __NAMESPACE__ constant returns name of current namespace__NAMESPACE ConstantFrom a named namespace, __NAMESPACE__ returns its name, for global and un-named namespace it returns empty striingExample Live Demo#test1.php OutputAn empty string is returnedname of global namespace :For named namespace, its name is returnedExample Live DemoOutputname of current namespace : myspaceDynamic name construction__NAMESPACE__ is useful for constructing name dynamicallyExample Live Demo

Advertisements