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 165 of 801
String replace multiple characters with an asterisk in JavaScript
We are required to write a JavaScript function that takes in a string as the first argument and an array of numbers. Our function should replace all the characters in the string at indices that are specified by the array elements taken as the second argument with an asterisk. Example The code for this will be − const str = "Lorem ipsum dolor sit amet consectetur adipiscing elit"; const arr = [4, 7, 9, 12, 15]; const replaceWithAsterisk = (str, indices) => { let res = ''; res ...
Read MoreGrouping and sorting 2-D array in JavaScript
Suppose we have a two-dimensional array of numbers like this − const arr = [ [1, 3, 2], [5, 2, 1, 4], [2, 1] ]; We are required to write a JavaScript function that groups all the identical numbers into their own separate subarray, and then the function should sort the group array to place the subarrays into increasing order. Therefore, finally the new array should look like − const output = [ [1, 1, 1], [2, 2, ...
Read MoreHow to sort array according to age in JavaScript?
We are required to write a JavaScript function that takes in an array of numbers representing ages of some people. Then the function should bring all the ages less than 18 to the front of the array without using any extra memory. Understanding the Problem We need to sort an array so that all minors (ages < 18) appear before adults (ages ≥ 18), while maintaining the original order within each group. Example The code for this will be − const ages = [23, 56, 56, 3, 67, 8, 4, 34, 23, 12, ...
Read MorePadding a string with random lowercase alphabets to fill length in JavaScript
We are required to write a function that takes in two arguments, first is a string and second is a number. The length of string is always less than or equal to the number. We have to insert some random lowercase alphabets at the end of the string so that its length becomes exactly equal to the number and we have to return the new string. Example Let's write the code for this function — const padString = (str, len) => { if(str.length < len){ ...
Read MoreJavaScript 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 MoreConsecutive elements sum array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as the sum of two consecutive elements from the original array. For example, if the input array is − const arr1 = [1, 1, 2, 7, 4, 5, 6, 7, 8, 9]; Then the output should be − const output = [2, 9, 9, 13, 17] How It Works The function pairs consecutive elements: (1+1=2), (2+7=9), (4+5=9), (6+7=13), (8+9=17). If the array has an odd length, the last ...
Read MoreFinding clusters of consecutive negative integers in JavaScript
We have an array of numbers like this: const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]; We are required to write a JavaScript function that counts the consecutive groups of negative numbers in the array. Understanding the Problem In the example array [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0], we have: First cluster: [-1, -2, -1] (positions 0-2) Second cluster: [-1, -2, -1, -2, -1] (positions 4-8) The function should return 2 since there are two separate groups of ...
Read MoreDifference of digits at alternate indices in JavaScript
In JavaScript, we can calculate the difference between the sum of digits at even indices and the sum of digits at odd indices. This is useful for various mathematical operations and data validation scenarios. Problem Statement We need to write a JavaScript function that takes a number and returns the absolute difference between the sum of digits at even positions and the sum of digits at odd positions (counting from right to left, starting at index 0). Example For the number 123456: Even indices (0, 2, 4): digits 6, 4, 2 → sum = 12 ...
Read MoreNegative number digit sum in JavaScript
We are required to write a JavaScript function that takes in a negative integer and returns the sum of its digits. Example Input For example: If the number is: -5456 Expected Output Then the output should be: 5+4+5+6 = 20 Using String Manipulation and Reduce The following approach converts the number to string, splits it into digits, and uses reduce to sum them: const num = -5456; const sumNum = num => { return String(num).split("").reduce((acc, val, ind) => { ...
Read MoreOmitting false values while constructing string in JavaScript
We have an array that contains some string values as well as some false values like null, undefined, 0, and empty strings. We need to write a JavaScript function that takes this array and returns a string constructed by joining values while omitting all falsy values. Understanding Falsy Values In JavaScript, falsy values include: false, 0, "", null, undefined, and NaN. These evaluate to false in boolean contexts. Method 1: Using reduce() with Logical OR The logical OR operator (||) returns the right operand when the left is falsy: const arr = ["Here", ...
Read More