AmitDiwan has Published 10744 Articles

Map an integer from decimal base to hexadecimal with custom mapping JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:40:29

384 Views

Usually when we convert a decimal to hexadecimal (base 16) we use the set 0123456789ABCDEF to map the number.We are required to write a function that does exactly the same but provides user the freedom to use any scale rather than the one mentioned above.For example −The hexadecimal notation of ... Read More

How to reduce an array while merging one of its field as well in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:39:58

498 Views

Consider, we have the following array of objects −const arr = [{    id: 121,    hobby: 'cycling' }, {    id: 125,    hobby: 'jogging' }, {    id: 129,    hobby: 'reading' }, {    id: 121,    hobby: 'writing' }, {    id: 121,    hobby: 'playing ... Read More

Determining happy numbers using recursion JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:37:52

774 Views

Happy NumberA happy number is a number which eventually reaches 1 when replaced by the sum of the square of each digit. Whereas if during this process any number gets repeated, the cycle will run infinitely and such numbers are called unhappy numbers.For example − 13 is a happy number ... Read More

Prefix sums (Creating an array with increasing sum) with Recursion in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:37:20

391 Views

Consider the following array of numbers −const arr = [10, 5, 6, 12, 7, 1];The sum of its consecutive elements taking one less element in every go will be −[10, 5, 6, 12, 7, 1] = 10 + 5 + 6 + 12 + 7 + 1 = 41; [5, ... Read More

How to slice an array with wrapping in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:35:28

364 Views

Let’s say, we are required to write an array method that overwrites the default Array.prototype.slice(). Usually the Array.prototype.slice() method takes in two arguments the start index and the end index, and returns a subarray of the original array from index start to end-1.What we wish to do is make this ... Read More

Get the item that appears the most times in an array JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:34:53

362 Views

Let’s say, we are required to write a function that takes in an array of string / number literals and returns the index of the item that appears for the most number of times. We will iterate over the array and prepare a frequencyMap and from that map we will ... Read More

How to write a JavaScript function that returns true if a portion of string 1 can be rearranged to string 2?

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:33:08

357 Views

We have to write a function that returns true if a portion of string1 can be rearranged to string2. Write function, say scramble(str1, str2) that returns true if a portion of str1 characters can be rearranged to match str2, otherwise returns false.For example −Let’s say string1 is str1 and string2 ... Read More

How to calculate total time between a list of entries?

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:32:12

175 Views

Let’s say, we have an array that contains some data about the speed of a motor boat during upstreams and downstreams like this −Following is our sample array −const arr = [{    direction: 'upstream',    velocity: 45 }, {    direction: 'downstream',    velocity: 15 }, {    direction: ... Read More

JavaScript Splitting string by given array element

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:31:10

180 Views

Let’s say, we are given a string and an array. Our job is to split the string according to the corresponding elements of the array. For example −Inputconst string = 'Javascript splitting string by given array element'; const arr = [2, 4, 5, 1, 3, 1, 2, 3, 7, 2];Output['Ja', ... Read More

Randomize color by number in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:28:08

147 Views

We are required to write a function that returns a random hex color. So here is the code for doing so −Exampleconst generateRandomColor = () => {    const keys = '0123456789ABCDEF';    let color = '';    while(color.length < 6){       const random = Math.floor(Math.random() * 16); ... Read More

Advertisements