An array of numbers is 100% shuffled if no two consecutive numbers appear together in the array (we only consider the ascending order case here). And it is 0% shuffled if pairs are of consecutive numbers.For an array of length n there will be n-1 pairs of elements (without distorting its order).We are required to write a JavaScript function that takes in an array of numbers and returns a number between [0, 100] representing the intensity of shuffle in the arrayExampleFollowing is the code −const arr = [4, 23, 1, 23, 35, 78, 4, 45, 7, 34, 7]; // this function calculates deviation from ascending sort const shuffleIntensity = arr => { let inCorrectPairs = 0; if(arr.length
We are required to write a JavaScript function that takes in a number n and returns a random string of length n containing no other than the 26 English lowercase alphabetsExampleLet us write the code for this function −const num = 8; const randomNameGenerator = num => { let res = ''; for(let i = 0; i < num; i++){ const random = Math.floor(Math.random() * 27); res += String.fromCharCode(97 + random); }; return res; }; console.log(randomNameGenerator(num));OutputFollowing is the output in the console −kdcwpingNote − This is one of many possible outputs. Console output is expected to differ every time/
We are required to write a JavaScript function that takes in a number n as the only input.The function should first find the current day (using Date Object in JavaScript) and then the function should return the day n days from today.For example −If today is Monday and n = 2, Then the output should be −WednesdayExampleFollowing is the code −const num = 15; const findNthDay = num => { const weekday=new Array(7); weekday[1]="Monday"; weekday[2]="Tuesday"; weekday[3]="Wednesday"; weekday[4]="Thursday"; weekday[5]="Friday"; weekday[6]="Saturday"; weekday[7]="Sunday" const day = new Date().getDay(); const daysFromNow = num % ... Read More
We are required to write a JavaScript function that takes in an array of Number / String literals and returns another array of arrays. With each subarray containing exactly two elements, the nth element from start nth from last.For example −If the array is −const arr = [1, 2, 3, 4, 5, 6];Then the output should be −const output = [[1, 6], [2, 5], [3, 4]];ExampleFollowing is the code −const arr = [1, 2, 3, 4, 5, 6]; const edgePairs = arr => { const res = []; const upto = arr.length % 2 === 0 ? arr.length ... Read More
IntroductionThere is a peculiar behaviour of finally block when either try block or catch block (or both) contain a return statement. Normally return statement causes control of program go back to calling position. However, in case of a function with try /catch block with return, statements in finally block are executed first before returning.ExampleIn following example, div() function has a try - catch - finally construct. The try block without exception returns result of division. In case of exception, catch block returns error message. However, in either case statement in finally block is executed first.Example Live DemoOutputFollowing output is displayedThis block ... Read More
IntroductionCode in finally block will always get executed whether there is an exception in ry block or not. This block appears either after catch block or instead of catch block.catch and finally blockIn following example, both catch and finally blocks are given. If execption occurs in try block, code in both is executed. If there is no exception, only finally block is executed.Example Live DemoOutputFollowing output is displayedCaught exception: Division by zero. This block is always executed Execution continueschange statement in try block so that no exception occursExample Live DemoOutputFollowing output is displayed2 This block is always executed Execution continuesfinally block onlyFollowing ... Read More
IntroductionException class implements Throwable interface and is base class for all Exception classes, predefined exceptions as well as user defined exceptions. The Exception class defines some final (non-overridable) methods to implement those from Throwable interface, and __tostring() method that can be overridden to return a string representation of Exception object.final public function getMessage()message of exceptionfinal public function getCode()code of exceptionfinal public function getFile()source filenamefinal public function getLine()source linefinal public function getTrace()an array of the backtrace()final public function getPrevious()previous exceptionfinal public function getTraceAsString()formatted string of tracepublic function __toString()formatted string for displayIf user defined exception class re-defines the constructor, it should call parent::__construct() to ensure ... Read More
We can select a drop-down menu option value with Selenium webdriver. The Select class in Selenium is used to handle drop-down. In an html document, the drop-down is identified with the tag.Let us see the html structure of a drop-down.For using the methods of Select class we have to import selenium.webdriver.support.select.Select in our code. Let us discuss the methods available to select an option from drop-down−select_by_visible_text (arg) – The arg which is passed as a parameter to the method is selected if it matches with the text which is visible in the dropdown.Syntax−sel = Select (driver.find_element_by_id ("name"))sel.select_by_visible_text ('Visible Text')select_by_value ... Read More
We can get text from each cell of an HTML table with Selenium webdriver.A tag is used to define a table in an html document. A table consists of rows represented by and columns represented by . The table headers are identified by tag.Let us consider a table from which we will get text from each cell.AUTOMATION TOOLTYPELINNKSeleniumOpen Sourcehttps://www.selenium.org/UFTCommercialUNified Functional TesterRanorexCommercialhttps://www.ranorex.com/TestCompleteCommercialTest CoompleteLet us see the html code representation for the above table−To retrieve the rows count of the table, we will use−List rows =driver.findElements(By.tagName("tr"));int rws_cnt= rows.size();To retrieve the columns count of the table, we will use−List cols ... Read More
We can get the entered text from a textbox in Selenium webdriver. To obtain the value attribute of an element in the html document, we have to use the getAttribute() method. Then the value is passed as a parameter to the method.Let us consider a textbox where we entered some text and then want to get the entered text.If we spy on the element, we will find that there is no value attribute for this element in the html code.After entering text inside that field, we can get the entered text by using the getAttribute() method.Exampleimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP