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 on Trending Technologies
Technical articles with clear explanations and examples
JavaScript Array: Checking for multiple values
We are required to write a JavaScript function that takes in two arrays of Numbers and checks whether all the elements of the first array exist in the second or not. Example The code for this will be — const arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89]; const multipleIncludes = (first, second) => { const indexArray = first.map(el => { return second.indexOf(el); }); return indexArray.indexOf(-1) === -1; } console.log(multipleIncludes(arr1, ...
Read MoreHow to define custom sort function in JavaScript?
Creating custom sort functions in JavaScript allows you to control exactly how array elements are ordered. By defining a custom sort function, you can sort elements in specific ways that go beyond the default string-based sorting behavior. This article will explain how to define custom sort functions in JavaScript, provide practical examples, and show different sorting techniques. Understanding the Default sort() Behavior By default, the sort() method converts elements to strings and compares them lexicographically. This can lead to unexpected results with numbers: ...
Read MoreFunction to check two strings and return common words in JavaScript
We are required to write a JavaScript function that takes in two strings as arguments. The function should then check the two strings for common substrings and prepare an array of those common parts. The function will find all possible substrings from the first string and check if they exist in the second string, returning an array of matches. Example const str1 = "IloveLinux"; const str2 = "weloveNodejs"; const findCommon = (str1 = '', str2 = '') => { const common = Object.create(null); let i, j, part; for (i = 0; i < str1.length - 1; i++) { for (j = i + 1; j
Read MoreRegular Expression Matching in JavaScript
Regular expression matching is a fundamental concept in computer science where we match an input string against a pattern containing special characters. In JavaScript, we can implement custom regex matching using dynamic programming to handle patterns with . (any character) and * (zero or more occurrences). Problem Definition Given an input string str and a pattern p, implement regular expression matching with support for: . → Matches any single character * → Matches zero or more of the preceding element The matching must cover the entire input string ...
Read MoreCounting the number of palindromes that can be constructed from a string in JavaScript
We are required to write a JavaScript function that takes in a string of characters as the first argument, say str, and a number, say num, as the second argument. The function should count the number of palindrome strings all exactly of length num can be constructed from the given string str. The function should then finally return the count. Problem Example If the input string and the number is: const str = 'ij'; const num = 4; Then the output should be: 4 because those four possible palindrome ...
Read MoreFinding n most frequent words from a sentence in JavaScript
For the purpose of this question, we define a sentence as a string that contains English alphabets and punctuations and a word is a substring of that sentence joined together by whitespaces. We are required to write a JavaScript function that takes in a sentence string, str, as the first argument and a number, num, as the second argument. The function should first count the frequency of each word in the sentence and then return an array of length num containing num most frequent words placed according to decreasing frequencies. Problem Example If the input sentence and ...
Read MoreRemoving punctuations from a string using JavaScript
Removing punctuation from strings is a common task in text processing applications. JavaScript provides several effective methods to accomplish this, from regular expressions to iterative approaches. Problem Statement Create a JavaScript function that removes all punctuation marks from a given string while preserving letters, numbers, and spaces. Sample Input: Hello, world! How's everything going? Sample Output: Hello world Hows everything going Using Regular Expression (Recommended) Regular expressions provide the most efficient way to remove punctuation. The pattern /[^\w\s]/g matches any character that isn't a word character (letters, digits) or whitespace. ...
Read MoreRemoving consecutive duplicates from strings in an array using JavaScript
Problem We are required to write a JavaScript function that takes in an array of strings. Our function should remove the duplicate characters that appear consecutively in the strings and return the new modified array of strings. Example Following is the code − const arr = ["kelless", "keenness"]; const removeConsecutiveDuplicates = (arr = []) => { const map = []; const res = []; arr.map(el => { el.split('').reduce((acc, value, index, arr) => { ...
Read Morecrypto.generateKeyPair() Method in Node.js
The crypto.generateKeyPair() method generates a new asymmetric key pair of the specified type. Supported types include RSA, DSA, EC, Ed25519, Ed448, X25519, X448 and DH. The function behaves as if keyObject.export has been called on its result when a publicKeyEncoding or privateKeyEncoding is specified, otherwise the respective part of keyObject is returned. Syntax crypto.generateKeyPair(type, options, callback) Parameters The above parameters are described as below: type – It holds the string type for which keys need to be generated. Supported types are - RSA, DSA, EC, ...
Read MoreHow to handle navigation from one page to another in react native?
While working on the app, we would like to switch from one screen to another and that is handled by React Navigation library. Installation To work on navigating pages we need to install few packages as follows: npm install @react-navigation/native @react-navigation/stack npm install @react-native-community/masked-view react-native-screens react-native-safe-area-context react-native-gesture-handler Once you are done with the above installation let us now proceed with the next setup of navigation in React Native. Creating Page Components In your app project create a folder called pages/. Create 2 js files HomePage.js and AboutPage.js. pages/HomePage.js import ...
Read More