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
Articles by AmitDiwan
Page 447 of 840
How to make Format ABC-1234 in JavaScript regular Expressions?
In JavaScript, you can format strings to match the pattern ABC-1234 using regular expressions. This pattern consists of three uppercase letters followed by a dash and four digits. Understanding the Pattern The ABC-1234 format requires: Three uppercase letters (A-Z) A dash (-) Four digits (0-9) Regular Expression Pattern The regex pattern for ABC-1234 format is: /^[A-Z]{3}-\d{4}$/ Breaking down this pattern: ^ - Start of string [A-Z]{3} - Exactly 3 uppercase letters - - Literal dash character \d{4} - Exactly 4 digits $ - End of string Method ...
Read MoreHow to assign static methods to a class in JavaScript?
To assign static methods to a class in JavaScript, prefix the method with the static keyword. Static methods belong to the class itself rather than instances and can be called without creating an object. Syntax class ClassName { static methodName() { // method body } } // Call static method ClassName.methodName(); Basic Static Method Example class Calculator { static add(a, b) { return a ...
Read MoreJavaScript program to take in a binary number as a string and returns its numerical equivalent in base 10
We are required to write a JavaScript function that takes in a binary number as a string and returns its numerical equivalent in base 10. Therefore, let's write the code for the function. This one is quite simple, we iterate over the string using a for loop and for each passing bit, we double the number with adding the current bit value to it like this − Example const binaryToDecimal = binaryStr => { let num = 0; for(let i = 0; i < binaryStr.length; i++){ ...
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 MoreSum of all prime numbers in an array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns the sum of all the prime numbers present in the array. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, 11 are prime numbers. Let's say the following is our array: const arr = [43, 6, 6, 5, 54, 81, 71, 56, 8, 877, 4, 4]; The function should sum the prime numbers: 43 + 5 + 71 ...
Read MoreHow to create and use a Syntax Highlighter with JavaScript?
A syntax highlighter in JavaScript allows you to apply styling to specific code elements like keywords, strings, or HTML tags. This tutorial shows how to create a basic highlighter using regular expressions and DOM manipulation. Example: HTML Link Highlighter This example highlights HTML anchor tags by applying custom CSS styles: body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .colorLinks { color: rgb(131, 44, 212); ...
Read MoreThe onchange event is not working in color type input with JavaScript
The onchange event works perfectly with color input elements in JavaScript. When a user selects a different color, the event triggers automatically, allowing you to capture and process the new color value. Syntax Basic Example Color Input onchange Event Choose Color: ...
Read MoreSetting property in an empty object using for loop in JavaScript.
In JavaScript, you can populate an empty object with properties using various loop methods. The most common approaches are the for...in loop and the for...of loop with Object.entries(). Using for...in Loop The for...in loop iterates over all enumerable properties of an object, making it ideal for copying properties from one object to another. Setting Properties with for...in Loop Setting Properties in Empty Object Populate ...
Read MoreJavaScript Compare two sentences word by word and return if they are substring of each other
The idea here is to take two strings as input and return true if one string is a substring of the other, otherwise return false. For example: isSubstr('hello', 'hello world') // true isSubstr('can I use', 'I us') // true isSubstr('can', 'no we are') // false In the function, we check which string is longer and then verify if the shorter string exists as a substring within the longer one. Syntax const isSubstr = (first, second) => { if (first.length > second.length) { ...
Read MoreHow can I filter JSON data with multiple objects?
To filter JSON data with multiple objects, you can use the filter() method along with comparison operators. This allows you to extract specific objects based on criteria. Syntax array.filter(callback(element, index, array)) Example: Filter by Single Property const jsonObject = [ { studentId: 101, studentName: "David" }, { studentId: 102, ...
Read More