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
Javascript Articles
Page 78 of 534
Determining isomorphic strings JavaScript
Two strings are isomorphic if characters in one string can be mapped to characters in another string while preserving the structure. Each character must map to exactly one character, and no two characters can map to the same character. Understanding Isomorphic Strings For strings to be isomorphic: They must have the same length Characters at the same positions must follow a consistent mapping pattern The mapping must be bijective (one-to-one) Example const str1 = 'egg'; const str2 = 'add'; // Check if strings are isomorphic const isIsomorphic = (str1 = '', str2 ...
Read MoreRemoving duplicates and keep one instance in JavaScript
In JavaScript, you often need to remove duplicate values from an array while keeping only one instance of each value. There are several effective methods to accomplish this task. Method 1: Using Set (Recommended) The most modern and efficient approach uses the Set object, which automatically removes duplicates: const arr = [1, 5, 7, 4, 1, 4, 4, 6, 4, 5, 8, 8]; // Using Set to remove duplicates const uniqueArr = [...new Set(arr)]; console.log(uniqueArr); [ 1, 5, 7, 4, 6, 8 ] Method 2: Using filter with indexOf ...
Read MoreSort an array and place a particular element as default value in JavaScript
We are required to write a JavaScript function that takes in an array of literal values as the first argument and a string as the second argument. Our function should sort the array alphabetically but keeping the string provided as the second argument (if it exists in the array) as the first element, irrespective of the text it contains. Example The code for this will be − const arr = ["Apple", "Orange", "Grapes", "Pineapple", "None", "Dates"]; const sortKeepingConstants = (arr = [], text = '') => { const sorter = (a, ...
Read MoreGrouping nested array in JavaScript
Suppose, we have an array of values like this − const arr = [ { value1:[1, 2], value2:[{type:'A'}, {type:'B'}] }, { value1:[3, 5], value2:[{type:'B'}, {type:'B'}] } ]; We are required to write a JavaScript function that takes in one such array. Our function should then prepare an array ...
Read MoreGet the max n values from an array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n, as the second argument. Our function should then pick the n greatest numbers from the array and return a new array consisting of those numbers. Using Sort and Slice The most straightforward approach is to sort the array in descending order and take the first n elements: const arr = [3, 4, 12, 1, 0, 5, 22, 20, 18, 30, 52]; const pickGreatest = (arr = [], num = 1) ...
Read MoreSorting elements of stack using JavaScript
We are required to write a JavaScript function that takes in an array of integers and sorts it using recursion with stack operations (push and pop methods). The function sorts the array in-place without using built-in sorting methods. How It Works The algorithm uses two recursive functions: sortStack() - Removes elements from the stack and recursively sorts the remaining elements sortedInsert() - Inserts an element in its correct position in an already sorted stack Example Here's the complete implementation: const stack = [-3, 14, 18, -5, 30]; const sortStack = (stack ...
Read MoreIndexing numbers to alphabets in JavaScript
We are required to write a JavaScript function that takes in a number between the range [0, 25], both inclusive and returns the corresponding alphabet character. Return Value The function should return the corresponding alphabet for that number, where 0 = 'A', 1 = 'B', 2 = 'C', and so on. Using String.fromCharCode() Method The most common approach uses ASCII values. The ASCII value of 'A' is 65, so we add our number to get the corresponding character. const num = 15; const indexToAlpha = (num = 1) => { ...
Read MoreReversing the alphabet from back to front and front to back in JavaScript
In JavaScript, we can create a function that finds the "mirror" position of an alphabet character. Given a letter, we find its position from the start and return the letter at the same position from the end. Understanding the Logic The alphabet has 26 letters. If we split it into two halves, each letter in the first half corresponds to a letter in the second half when reversed. For example, 'a' (1st position) corresponds to 'z' (26th position), 'b' corresponds to 'y', and so on. Example Implementation Here's how we can implement this functionality: ...
Read MoreUsing a recursive function to capitalize each word in an array in JavaScript
We are required to write a JavaScript function that takes in an array of String literals. The function should do the following two things − Make use of recursive approach Make first word of each string element capital. Our function should do this without using extra space for storing another array. For example − If the input array is − const arr = ['apple', 'banana', 'orange', 'grapes']; Then the array should be transformed to − const output = ['Apple', 'Banana', 'Orange', 'Grapes']; ...
Read MoreDifference between first and the second array in JavaScript
When working with arrays in JavaScript, you often need to find elements that exist in one array but not in another. This operation is commonly known as finding the "difference" between two arrays. We'll create a function that takes two arrays and returns all elements from the first array that don't exist in the second array. Example Here's how to implement this functionality: const arr1 = ['1', '2', '3', '4/2', '5/4', '6-2']; const arr2 = ['1', '2', '3', '5/4', '4/2', '6-1', '7/2', '8-2']; const differenceBetween = (arr1 = [], arr2 = []) => { ...
Read More