Found 10483 Articles for Web Development

Sort Array of numeric & alphabetical elements (Natural Sort) JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:50:19

1K+ Views

We have an array that contains some numbers and some strings. We are required to sort the array such that the numbers gets sorted and get placed before every string and then the string should be placed sorted alphabetically.For example −This array after being sortedconst arr = [1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9];Should look like this −[1, 6, 7, 9, 47, 'afv', 'bdf', 'fdf', 'svd']So, let’s write the code for this −Exampleconst arr = [1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9]; const sorter = (a, b) => {    if(typeof a === 'number' && typeof ... Read More

Validate input: replace all ‘a’ with ‘@’ and ‘i’ with ‘!’JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:48:43

111 Views

We are required to write a function validate() that takes in a string as one and only argument and returns another string that has all ‘a’ and ‘i’ replaced with ‘@’ and ‘!’ respectively.It’s one of those classic for loop problems where we iterate over the string with its index and construct a new string as we move through.The code for the function will be −Exampleconst string = 'Hello, is it raining in Amsterdam?'; const validate = (str) => {    let validatedString = '';    for(let i = 0; i < str.length; i++){       if(str[i] === 'a'){ ... Read More

Finding nearest Gapful number in JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:46:32

275 Views

A number is a gapful number when −It has at least three digits, andIt is exactly divisible by the number formed by putting its first and last digits togetherFor example −The number 1053 is a gapful number because it has 4 digits and it is exactly divisible by 13. Similarly, 135 is a gapful number because it has 3 digits and it is exactly divisible by 15.Our job is to write a program that returns the nearest gapful number to the number we provide as input.For example, for all 2-digit numbers, it would be 100. For 103, it would be ... Read More

How to remove “,” from a string in JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:45:08

171 Views

We are given a main string and a substring, our job is to create a function shedString() that takes in these two arguments and returns a version of the main string which is free of the substring.For example −shedString('12/23/2020', '/');should return a string −'12232020'Let’s now write the code for this function −Exampleconst shedString = (string, separator) => {    //we split the string and make it free of separator    const separatedArray = string.split(separator);    //we join the separatedArray with empty string    const separatedString = separatedArray.join("");    return separatedString; } const str = shedString('12/23/2020', '/'); console.log(str);OutputThe output of this ... Read More

Create a polyfill to replace nth occurrence of a string JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:43:56

503 Views

Let’s say, we have created a polyfill function removeStr() that takes in three arguments, namely −subStr → the occurrence of which String is to be removednum → it’s a Number (num)th occurrence of subStr is to be removed from stringThe function should return the new if the removal of the subStr from the string was successfully, otherwise it should return -1 in all cases.For example −const str = 'drsfgdrrtdr'; console.log(str.removeStr('dr', 3));Expected output −'drsfgdrrt'Let’s write the code for this −Exampleconst str = 'drsfgdrrtdr'; const subStr = 'dr'; const num = 2; removeStr = function(subStr, num){    if(!this.includes(subStr)){       return ... Read More

Finding out the Harshad number JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:42:30

575 Views

Harshad numbers are those numbers which are exactly divisible by the sum of their digits. Like the number 126, it is completely divisible by 1+2+6 = 9.All single digit numbers are harshad numbers.Harshad numbers often exist in consecutive clusters like [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [110, 111, 112], [1010, 1011, 1012].Our job is to write a function that takes in a Number as input checks whether it’s a harshad number or not, if not then returns -1 otherwise it returns the length of streak of consecutive harshad cluster.For example −harshadNum(1014) = harshadNum(1015) = harshadNum(1016) = ... Read More

How to create a random number between a range JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:39:28

249 Views

Our job is to create a function, say createRandom, that takes in two argument and returns a pseudorandom number between the range (max exclusive).The code for the function will be −Exampleconst min = 3; const max = 9; const createRandom = (min, max) => {    const diff = max - min;    const random = Math.random();    return Math.floor((random * diff) + min); } console.log(createRandom(min, max));Understanding the code −We take the difference of max and minWe create a random numberThen we multiply the diff and random to produce random number between 0 and diffThen we add min to it ... Read More

How to exclude certain values from randomly generated array JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:38:09

1K+ Views

We have to create a function that takes in 2 arguments: an integer and an array of integers. First argument denotes the length of array we have to return and the second argument contains the elements that should not be present in our return array. Actually, we need an array of random numbers between 0 to 100 but it should not include any element that’s present in the argument array.Note − No two numbers should be duplicate.Let’s call our function generateRandom(). The code for this would be −Exampleconst absentArray = [44, 65, 5, 34, 87, 42, 8, 76, 21, 33]; ... Read More

JavaScript - Converting array of objects into object of arrays

AmitDiwan
Updated on 28-Jan-2025 14:56:36

1K+ Views

In this article, we will learn to convert an array of objects into an object of arrays in JavaScript. When working with JavaScript, it's common to handle arrays of objects representing structured data. A frequent challenge is grouping these objects based on a shared property and transforming the result into a new data structure. Problem Statement The goal is to convert an array of objects into an object of arrays where the keys represent a specific property (e.g., roles) and the values are arrays of corresponding data (e.g., player names). Input const team = [ { role: ... Read More

JavaScript array.includes inside nested array returning false where as searched name is in array

AmitDiwan
Updated on 18-Aug-2020 07:43:39

1K+ Views

It is a well-known dilemma that when we use includes() inside nested arrays i.e., multidimensional array, it does not work, there exists a Array.prototype.flat() function which flats the array and then searches through but it’s browser support is not very good yet.So our job is to create a includesMultiDimension() function that takes in an array and a string and returns a boolean based on the presence/absence of that string in the array.There exist many solutions to this problem, most of them includes recursion, heavy array function, loops and what not.What we are going to discuss here is by far the ... Read More

Advertisements