Looping Through and Getting Frequency of Elements in an Array in JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:52:47

864 Views

Let’s say, we will be given an array of numbers / strings that contains some duplicate entries, all we have to do is to return the frequency of each element in the array. Returning an object with element as key and it’s value as frequency would be perfect for this situation.We will iterate over the array with a forEach() loop and keep increasing the count of elements in the object if it already exists otherwise we will create a new property for that element in the object.And lastly, we will return the object.The full code for this problem will be ... Read More

Add Line Break Inside String in JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:51:34

499 Views

We are required to write a function, say breakString() that takes in two arguments: First, the string to be broken and second, a number that represents the threshold count of characters after reaching which we have to repeatedly add line breaks in place of spaces.For example −The following code should push a line break at the nearest space if 4 characters have passed without a line break −const text = 'Hey can I call you by your name?'; console.log(breakString(text, 4));Expected Output −Hey can I call you by your name?So, we will iterate over the with a for loop, we will ... Read More

Sort Array of Numeric and Alphabetical Elements Using Natural Sort in 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 'i' Using JavaScript

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

105 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

272 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

Remove Substring from a String in JavaScript

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

169 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 in JavaScript

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

501 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 in JavaScript

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

573 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

Create Random Number Between a Range in JavaScript

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

247 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

Exclude Certain Values from Randomly Generated Array in 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

Advertisements