AmitDiwan has Published 10744 Articles

JavaScript - Writing a string function to replace the kth appearance of a character

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 12:39:10

242 Views

Let’s say, we are required to write a String.prototype function that takes in three arguments.First argument is string that should be searched for substringsSecond argument is the string, the occurrence of which String to be removedThird argument is a Number say n, nth occurrence of substring to be removed from string.The ... Read More

Prime numbers upto n - JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 12:37:43

631 Views

Let’s say, we are required to write a JavaScript function that takes in a number, say n, and returns an array containing all the prime numbers upto n.For example − If the number n is 24, then the output should be −const output = [2, 3, 5, 7, 11, 13, ... Read More

Merging boolean array with AND operator - JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 12:36:07

771 Views

Let’s say, we have an array of arrays of boolean like this −const arr = [[true, false, false], [false, false, false], [false, false, true]];We are required to write a function that merges this array of arrays into a one-dimensional array by combining the corresponding elements of each subarray using the ... Read More

Finding the difference between two arrays - JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 12:34:55

553 Views

We have two arrays of numbers like these −const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34];We are required to write a JavaScript function that takes in two such arrays and returns the element from arrays that are ... Read More

Counting below / par elements from an array - JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 12:31:13

183 Views

We are required to write a function that counts how many of the elements are in the array below / above a given number.Following is our array of Numbers −const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78];For example, if the number is 60, ... Read More

Code to find the center of an array without using ES6 functions - JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 12:30:04

330 Views

We are required to write an array function midElement() that returns the middlemost element of the array without accessing its length property and without using any kind of built-in loops.If the array contains an odd number of elements, we return the one, middlemost element, or if the array contains an ... Read More

Summing all the unique values of an array - JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 12:29:05

287 Views

Let’s say, we are required to write a JavaScript function that takes in an array of numbers with duplicate entries and sums all the duplicate entries to one index.For example −If the input array is −const input = [1, 3, 1, 3, 5, 7, 5, 4];Then the output should be ... Read More

Dividing an array – JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 12:27:30

604 Views

Let’s say, we are required to write a function that takes in an array arr of string / number literals as the first argument and a number n as second argument.We are required to return an array of n subarrays, each of which contains at most arr.length / n elements. ... Read More

Finding the intersection of arrays of strings - JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 10:02:47

746 Views

We have two arrays of Numbers, and we are required to write a function, let’s say intersection() that computes their intersection and returns an array that contains the intersecting elements in any order. Each element in the result should appear as many times as it shows in both arrays.For example ... Read More

Counting the clusters of positive numbers - JavaScript Arrays

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 10:01:40

267 Views

Let’s say, 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 non-negative (positives and 0) numbers in the array.Like here we have consecutive ... Read More

Advertisements