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 177 of 801
Sort 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 MoreChecking the validity of parentheses in JavaScript
We are required to write a JavaScript function that takes in a string containing just the characters: '(', ')', '{', '}', '[' and ']' Our function should determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. ...
Read MoreDash separated cartesian product of any number of arrays in JavaScript
We need to write a JavaScript function that takes any number of arrays and computes their cartesian product, with elements separated by dashes. The cartesian product combines every element from the first array with every element from the second array, and so on. What is Cartesian Product? The cartesian product of two or more sets is the set of all possible combinations where we pick one element from each set. For example, if we have arrays ['a', 'b'] and ['1', '2'], the cartesian product would be ['a-1', 'a-2', 'b-1', 'b-2']. Implementation Here's how we can implement ...
Read More