AmitDiwan has Published 10744 Articles

Recursive sum all the digits of a number JavaScript

AmitDiwan

AmitDiwan

Updated on 19-Aug-2020 07:02:55

2K+ Views

Let’s say, we are required to create a function that takes in a number and finds the sum of its digits recursively until the sum is a one-digit number.For example −findSum(12345) = 1+2+3+4+5 = 15 = 1+5 = 6So, the output should be 6.Let’s write the code for this function ... Read More

How to sum elements at the same index in array of arrays into a single array? JavaScript

AmitDiwan

AmitDiwan

Updated on 19-Aug-2020 07:01:48

1K+ Views

We have an array of arrays and are required to write a function that takes in this array and returns a new array that represents the sum of corresponding elements of original array.If the original array is −[    [43, 2, 21], [1, 2, 4, 54], [5, 84, 2], [11, ... Read More

Group objects inside the nested array JavaScript

AmitDiwan

AmitDiwan

Updated on 19-Aug-2020 07:00:09

2K+ Views

Let’s say e have a parentArray that contains many sub arrays each of the same size, each sub array is an array of objects containing two properties: key and value. Within a subarray it is confirmed that two objects cannot have the same key but all subarrays have the same ... Read More

Recursively list nested object keys JavaScript

AmitDiwan

AmitDiwan

Updated on 19-Aug-2020 06:57:39

6K+ Views

Let’s say, we have an object with other objects being its property value, it is nested to 2-3 levels or even more.Here is the sample object −const people = {    Ram: {       fullName: 'Ram Kumar',       details: {          age: 31, ... Read More

Grouping on the basis of object property JavaScript

AmitDiwan

AmitDiwan

Updated on 19-Aug-2020 06:55:56

146 Views

We have an array of objects that contains data about some cars. The array is given as follows −const cars = [{    company: 'Honda',    type: 'SUV' }, {    company: 'Hyundai',    type: 'Sedan' }, {    company: 'Suzuki',    type: 'Sedan' }, {    company: 'Audi',   ... Read More

Converting Odd and Even-indexed characters in a string to uppercase/lowercase in JavaScript?

AmitDiwan

AmitDiwan

Updated on 19-Aug-2020 06:53:57

2K+ Views

We need to write a function that reads a string and converts the odd indexed characters in the string to upperCase and the even ones to lowerCase and returns a new string.Full code for doing the same will be −Exampleconst text = 'Hello world, it is so nice to be ... Read More

Looping through and getting frequency of all the elements in an array JavaScript

AmitDiwan

AmitDiwan

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

865 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 ... Read More

Add line break inside string only between specific position and only if there is a white space JavaScript

AmitDiwan

AmitDiwan

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

501 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 ... Read More

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

AmitDiwan

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', ... Read More

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

AmitDiwan

AmitDiwan

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

107 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 ... Read More

Advertisements