Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Web Development Articles
Page 429 of 801
Vowel, other characters and consonant difference in a string JavaScript
We are required to write a function that takes in a string of definite characters and the function should return the difference between the count of vowels plus other characters and consonants in the string. Problem Explanation For example, if the string is: "HEllo World!!" Then, we have 7 consonants, 3 vowels and 3 other characters here so the output should be: |7 - (3+3)| = 1 Hence, the output should be 1 Example const str = 'HEllo World!!'; const findDifference = str => { const creds ...
Read MoreExplain the callback Promise.finally in JavaScript
The Promise.finally() method executes a callback function when a promise settles, regardless of whether it's fulfilled or rejected. This method is essential for cleanup operations and tasks that must run after promise completion. Syntax promise.finally(callback) Parameters callback: A function that executes when the promise settles. It receives no arguments and its return value is ignored. How It Works Unlike .then() and .catch(), the finally callback doesn't receive the promise's result or rejection reason. It simply indicates that the promise has completed. Example with Fetch API ...
Read MoreReturn the difference between the maximum & minimum number formed out of the number n in JavaScript
We have to write a function that takes a positive number n and returns the difference between the maximum number and the minimum number that can be formed from its digits. For example, if the number n is 203: The maximum number that can be formed from its digits will be 320 The minimum number that can be formed from its digits will be 23 (placing the zero at one's place) And the difference will be: 320 - 23 = 297 Therefore, the output should ...
Read MoreExplain 'dotAll' flag for regular expressions in JavaScript
The dotAll flag returns true or false depending upon if the s flag has been set in the regular expression. The s flag enables "dotAll" mode, where the dot . metacharacter matches any character, including newlines. What is the dotAll Flag? By default, the dot . in regular expressions matches any character except newlines. When the s flag is used, the dot can also match newline characters (, \r, etc.), making it truly match "any" character. Syntax regex.dotAll // Returns true if 's' flag is set, false otherwise Example: Checking dotAll Flag ...
Read MoreReturn Top two elements from array JavaScript
We have an array of numbers in JavaScript that contains numbers in an unsorted order. Our job is to write a function that takes in this array of numbers and returns an array of two elements, the top two elements of the array (greatest two elements of the array). We have to do this in one pass i.e., we need to execute this method in linear time like by using only one for loop or if we use ES6 function, we have to make sure to use only one and once and avoid nesting of methods which increases time ...
Read MoreHow to import and export a module/library in JavaScript?
Note − To run this example you will need to run a localhost server. JavaScript modules allow you to break code into separate files and share functionality between them using export and import statements. This promotes code reusability and better organization. Example INDEX.html Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...
Read MoreNaN and Infinity example in JavaScript
In JavaScript, NaN (Not-a-Number) and Infinity are special numeric values that represent invalid or infinite calculations. Understanding these values is crucial for handling mathematical operations and validation. What is NaN? NaN represents a value that is "Not-a-Number". It occurs when a mathematical operation fails or produces an undefined result. NaN Examples let results = document.getElementById('nanResults'); ...
Read MoreReverse all the words of sentence JavaScript
We are required to write a JavaScript function that takes in a string and returns a new string which has all the words reversed from the original string. For example − If the original string is − "Hello World how is it outside" Then the output should be − "olleH dlroW woh si ti edistuo" Now, let's write the code for this function − Example const str = 'Hello World how is it outside'; const reverseSentence = str => { const arr = ...
Read MoreHow to search for a string in JavaScript?
JavaScript provides several methods to search for strings. The most common approaches are search(), indexOf(), includes(), and match(). Using search() Method The search() method returns the index of the first match or -1 if not found. It supports regular expressions. String Search Example The spring season is about to come. SEARCH FOR 'spring' ...
Read MoreHow to print positive and negative infinity values in JavaScript?
In JavaScript, infinity values represent numbers that exceed the maximum finite value. JavaScript provides built-in constants to handle positive and negative infinity values. JavaScript Infinity Constants JavaScript has two built-in constants for infinity values: Infinity or Number.POSITIVE_INFINITY - represents positive infinity -Infinity or Number.NEGATIVE_INFINITY - represents negative infinity Basic Example JavaScript Infinity Values JavaScript Infinity Values ...
Read More