Sorting Digits of All Numbers in Array Using JavaScript

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

792 Views

We are required to write a JavaScript function that takes in an array of numbers and reorders the digit of all the numbers internally in a specific order (lets say in ascending order for the sake of this problem).For example − If the array is −const arr = [543, 65, 343, 75, 567, 878, 87];Then the output should be −const output = [345, 56, 334, 57, 567, 788, 78];ExampleFollowing is the code −const arr = [543, 65, 343, 75, 567, 878, 87]; const ascendNumber = num => {    const numArr = String(num).split('').map(el => +el);    numArr.sort((a, b) => a ... Read More

Get Current Contents of an Element in WebDriver

Debomita Bhattacharjee
Updated on 18-Sep-2020 09:09:28

467 Views

We can get the current contents of an element in Selenium webdriver. For the elements having tagname as we have to use the getAttribute() method and pass the value parameter as an argument to that method to obtain the current contents.For the elements without an input tag we have to use the getText() method to obtain the current contents. First of all we have to identify the element with the help of the locators.Let us try to get the content Selenium inside the edit box and its above text content – You are browsing the best resource for Online ... Read More

Count Redundant Characters in a String using JavaScript

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

284 Views

We are required to write a JavaScript function that takes in a string and returns the count of redundant characters in the string.For example − If the string is −const str = 'abcde'Then the output should be 0If the string is −const str = 'aaacbfsc';Then the output should be 3ExampleFollowing is the code −const str = 'aaacbfsc'; const countRedundant = str => {    let count = 0;    for(let i = 0; i < str.length; i++){       if(i === str.lastIndexOf(str[i])){          continue;       };       count++;    };    return count; }; console.log(countRedundant(str));OutputFollowing is the output in the console −3

Checking Progressive Array in JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:07:43

210 Views

We are required to write a JavaScript function that takes in an array of strings, ordered by ascending length.The function should return true if, for each pair of consecutive strings, the second string can be formed from the first by adding a single letter either at the beginning or end.For example: If the array is given by −const arr = ["c", "ca", "can", "acan", "acane", "dacane"];Then our function should return true.ExampleFollowing is the code −const arr = ["c", "ca", "can", "acan", "acane", "dacane"]; const isProgressive = arr => {    for(let i = 0; i < arr.length-1; i++){     ... Read More

PHP Nested Exception Handling

Malhar Lathkar
Updated on 18-Sep-2020 09:04:43

628 Views

IntroductionBlocks of try - catch can be nested upto any desired levels. Exceptions will be handled in reverse order of appearance i.e. innermost exception processing is done first.ExampleIn following example,inner try block checks if either of two varibles are non-numeric, nd if so, throws a user defined exception. Outer try block throws DivisionByZeroError if denominator is 0. Otherwise division of two numbers is displayed.Example Live DemoOutputFollowing output is displayedDivision by 0 in line no 19Change any one of varibles to non-numeric valueerror : Non numeric data in line no 20If both variables are numbers, their division is printed

Check Element Visibility with WebDriver

Debomita Bhattacharjee
Updated on 18-Sep-2020 09:03:11

8K+ Views

We can check if an element exists with Selenium webdriver. There are multiple ways to check it. We shall use the explicit wait concept in synchronization to verify the visibility of an element.Let us consider the below webelement and check if it is visible on the page. There is a condition called visibilityOfElementLocated which we will use to check for element visibility. It shall wait for a specified amount of time for the element after which it shall throw an exception.We need to import org.openqa.selenium.support.ui.ExpectedConditions and import org.openqa.selenium.support.ui.WebDriverWait to incorporate expected conditions and WebDriverWait class. We will introduce a try/catch ... Read More

Returning Poker Pair Cards in JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:02:15

166 Views

We are required to write a function that takes in an array of exactly five elements representing the five cards of a poker player drawn randomly.If the five cards contain at least one pair, our function should return the card number of the highest pair (trivial if there only exists a single pair). Else our function should return false.For example: If the array is −const arr = ['A', 'Q', '3', 'A', 'Q'];Then our function should return −'A'  (as 'A' > 'Q' in card games)ExampleFollowing is the code −const arr = ['A', 'Q', '3', 'A', 'Q']; const greatestPair = arr => ... Read More

PHP Exception Handling with Multiple Catch Blocks

Malhar Lathkar
Updated on 18-Sep-2020 09:00:52

2K+ Views

IntroductionPHP allows a series of catch blocks following a try block to handle different exception cases. Various catch blocks may be employed to handle predefined exceptions and errors as well as user defined exceptions.ExampleFollowing example uses catch blocks to process DivisioByZeroError, TypeError, ArgumentCountError and InvalidArgumentException conditions. There is also a catch block to handle general Exception.Example Live DemoOutputTo begin with, since denominator is 0, Divide by 0 error will be displayedDivision by 0Set $b=3 which will cause TypeError because divide function is expected to return integer but dividion results in floatReturn value of divide() must be of the type integer, float ... Read More

Finding Closed Loops in a Number Using JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:00:51

528 Views

Other than all being a natural number, the numbers 0, 4, 6, 8, 9 have one more thing in common. All these numbers are formed by or contain at least one closed loop in their shapes.For example, the number 0 is a closed loop, 8 contains two closed loops and 4, 6, 9 each contains one closed loop.We are required to write a JavaScript function that takes in a number and returns the sum of the closed loops in all its digits.For example, if the number is 4789Then the output should be 4 i.e.1 + 0 + 2 + 1ExampleFollowing is ... Read More

Remove Last Vowel from String in JavaScript

AmitDiwan
Updated on 18-Sep-2020 08:59:33

369 Views

We are required to write a JavaScript function that takes in a string and returns a new string with the last vowel of each word removed.For example − If the string is −const str = 'This is an example string';Then the output should be −const output = 'Ths s n exampl strng';ExampleFollowing is the code −const str = 'This is an example string'; const removeLast = word => {    const lastIndex = el => word.lastIndexOf(el);    const ind = Math.max(lastIndex('a'), lastIndex('e'), lastIndex('i'),    lastIndex('o'), lastIndex('u'));    return word.substr(0, ind) + word.substr(ind+1, word.length); } const removeLastVowel = str => { ... Read More

Advertisements