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
Front End Technology Articles
Page 378 of 652
Random name generator function in JavaScript
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 alphabets. Example Let 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() * 26); res += String.fromCharCode(97 + ...
Read MoreHow to decode an encoded string in JavaScript?
In JavaScript, string decoding refers to converting encoded strings back to their original form. While escape() and unescape() were historically used, modern JavaScript provides better alternatives like decodeURIComponent() and decodeURI(). Using unescape() (Deprecated) The unescape() method decodes strings encoded by the escape() method. It replaces hexadecimal escape sequences with their corresponding characters. Syntax unescape(string) Example // Special character encoded with escape function var str = escape("Tutorialspoint!!"); document.write("Encoded : " + str); document.write(""); ...
Read MoreNearest Prime to a number - JavaScript
We are required to write a JavaScript function that takes in a number and returns the first prime number that appears after n. For example: If the number is 24, then the output should be 29. Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples include 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, etc. Implementation We'll create two functions: one to check if a number is prime, and another to find the nearest prime after a given ...
Read MoreHow to parse an URL in JavaScript?
JavaScript provides several methods to parse URLs and extract their components like protocol, hostname, pathname, and query parameters. The modern approach uses the built-in URL constructor, while legacy methods involve creating DOM anchor elements. Using the URL Constructor (Modern Approach) The URL constructor is the standard way to parse URLs in modern JavaScript. It creates a URL object with all components easily accessible. const url = new URL("https://www.youtube.com/watch?v=tNJJSrfKYwQ"); console.log("Protocol:", url.protocol); console.log("Host:", url.host); ...
Read MoreHow to add two strings with a space in first string in JavaScript?
To concatenate two strings in JavaScript, we use the '+' operator. When the first string already contains a trailing space, we can directly concatenate without adding an explicit space. However, if there's no space, we need to add one manually between the strings. Method 1: First String Has Trailing Space When the first string already ends with a space, simple concatenation is sufficient: function concatenateStrings(str1, str2) { return (str1 ...
Read MoreReturning poker pair cards - JavaScript
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) Card Ranking System ...
Read MoreWhat is the use of Object.is() method in JavaScript?
The Object.is() method determines whether two values are strictly equal, providing more precise comparison than the regular equality operators. Syntax Object.is(value1, value2) Parameters value1: The first value to compare value2: The second value to compare Return Value Returns true if both values are the same, false otherwise. How Object.is() Determines Equality Two values are considered the same when they meet these criteria: Both are undefined or both are null Both are true or both ...
Read MoreCounting the number of redundant characters in a string - JavaScript
We are required to write a JavaScript function that takes in a string and returns the count of redundant characters in the string. A redundant character is any character that appears more than once, where all duplicate occurrences (except the last one) are considered redundant. For example − If the string is − const str = 'abcde' Then the output should be 0 because each character appears only once. If the string is − const str = 'aaacbfsc'; Then the output should be 3 because 'a' appears 3 times (2 ...
Read MoreHow many ways can a property of a JavaScript object be accessed?
JavaScript object properties can be accessed in two main ways: dot notation and bracket notation. Each method has its own use cases and advantages. Dot Notation The dot notation is the most common and readable way to access object properties when the property name is known at compile time. object.propertyName Bracket Notation The bracket notation uses square brackets with the property name as a string. This method is more flexible and allows dynamic property access. object["propertyName"] object[variableName] Example: Using Dot Notation var person = ...
Read MoreSorting digits of all the number of array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and reorders the digits of all the numbers internally in a specific order (let's 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]; Approach The solution involves converting each number to a string, splitting it into individual digits, sorting those ...
Read More