ASCII CodeASCII is a 7-bit character code where every single bit represents a unique character.Every English alphabet has a unique decimal ascii code.We are required to write a JavaScript function that takes in a string and counts the sum of all the ascii codes of the string charactersExampleFollowing is the code −const str = 'This string will be used for calculating ascii score'; const calculateASCII = str => { let res = 0; for(let i = 0; i < str.length; i++){ const num = str[i].charCodeAt(0); res += num; }; return res; }; console.log(calculateASCII(str));OutputFollowing is the output in the console −4946
IntroductionClass is a user defined data type in PHP. In order to define a new class, PHP provides a keyword class, which is followed by a name. Any label that is valid as per PHP's naming convention (excluding PHP's reserved words) can be used as name of class. Constituents of class are defined in curly bracket that follows name of classSyntaxclass myclass{ // }Class may contain constants, variables or properties and methods - which are similar to functionsExample of classThis example shows how a Class is definedExampleFunction defined inside class is called method. Calling object's context is available inside ... Read More
Conventionally, we have functions like parseInt() and parseFloat() that takes in a string and converts the number string to Number. But these methods fail when we have numbers embedded at random index inside the string.For example: The following would only return 454, but what we want is 4545453 −parseInt('454ffdg54hg53')So, we are required to write a JavaScript function that takes in such string and returns the corresponding number.ExampleFollowing is the code −const numStr = '454ffdg54hg53'; const parseInteger = numStr => { let res = 0; for(let i = 0; i < numStr.length; i++){ if(!+numStr[i]){ ... Read More
We are required to write a JavaScript function that takes in two arrays of numbers of the same length. The function should return an array with any arbitrary nth element of the array being the sum of nth term from start of first array and nth term from last of second array.For example −If the two arrays are −const arr1 = [34, 5, 3, 3, 1, 6]; const arr2 = [5, 67, 8, 2, 6, 4];Then the output should be −const output = [38, 11, 5, 11, 68, 11];ExampleFollowing is the code −const arr1 = [34, 5, 3, 3, 1, ... Read More
IntroductionIn order to use class defined in another PHP script, we can incorporate it with include or require statements. However, PHP's autoloading feature doesn't need such explicit inclusion. Instead, when a class is used (for declaring its object etc.) PHP parser loads it automatically, if it is registered with spl_autoload_register() function. Any number of classes can thus be registered. This way PHP parser gets a laast chance to load class/interface before emitting error.Syntaxspl_autoload_register(function ($class_name) { include $class_name . '.php'; });The class will be loaded from its corresponding .php file when it comes in use for first timeAutoloading exampleThis example ... Read More
We have to write a function, that does some simple task, say adding two numbers or something like that. We are required to demonstrate the way we can access the variables declared inside that function in some other function or globally.ExampleFollowing is the code −const num = 5; const addRandomToNumber = function(num){ // a random number between [0, 10) const random = Math.floor(Math.random() * 10); // assigning the random to this object of function // so that we can access it outside this.random = random; this.res = num + random; }; const addRandomInstance = ... Read More
If we look closely at the problem statement, the difference between ([] == false) and ([] || true) is the following −In the first case, we are using loose conditional checking, allowing type coercion to take over.While in the second case, we are evaluating [] to its respective Boolean (truthy or falsy) which makes use of the function Boolean() instead of type coercion under the hook.Let's now unveil the conversions that happens behind the scenes in both cases.Case 1 − ([] == false)According to the MDN docs if two data types say x and y are compared using the loose ... Read More
We are required to sort a dynamic JavaScript array. The condition is that we are required to sort it according to the values stored in a particular order in a standard predefined array.Let’s say the following is our dynamic array −const dbArray = ['Apple', 'Banana', 'Mango', 'Apple', 'Mango', 'Mango', 'Apple'];And suppose the standard array against which we have to sort the above array is like −const stdArray = ['Mango', 'Apple', 'Banana', 'Grapes'];So, after sorting the dbArray, my resultant array should look like −const resultArray = ['Mango', 'Mango', 'Mango', 'Apple', 'Apple', 'Apple', 'Banana'];ExampleFollowing is the code −const dbArray = ['Apple', 'Banana', ... Read More
We can get the value of an input box with Selenium webdriver. The get_attribute() method is capable of obtaining the value we have entered in an input box. To get the value, we have to pass value as a parameter to the method.First of all, we have to identify the input box with the help of any of the locators like id, class, name, css or xpath. Then we have to type some values inside it with the send_keys() method.Let us consider the below input box where we shall enter some texts - Selenium Python and then fetch the value ... Read More
We are required to write a JavaScript function that takes in a string as first argument and two single element strings. The function should return the distance between those single letter stings in the string taken as first argument.For example −If the three strings are −const str = 'Disaster management'; const a = 'i', b = 't';Then the output should be 4 because the distance between 'i' and 't' is 4ExampleFollowing is the code −const str = 'Disaster management'; const a = 'i', b = 't'; const distanceBetween = (str, a, b) => { const aIndex = str.indexOf(a); ... Read More