AmitDiwan has Published 10744 Articles

Retrieve user id from array of object - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 13:44:49

901 Views

Suppose, we have an array of objects where the user names are mapped to some unique ids like this −const arr = [    {"4": "Rahul"},    {"7": "Vikram"},    {"6": "Rahul"},    {"3": "Aakash"},    {"5": "Vikram"} ];As apparent in the array, same names can have more than one ... Read More

Find the shortest string in an array - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 13:43:24

732 Views

We are required to write a JavaScript function that takes in an array of strings and returns the index of string that is shortest in length.We will simply use a for loop and persist the index of string which is shortest in length.ExampleFollowing is the code −const arr = ['this', ... Read More

Sum all similar elements in one array - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 13:41:53

427 Views

We are required to write a JavaScript function that takes in an array of Numbers and sums all the identical numbers together at one indexFor example −If the input array is −const arr = [20, 10, 15, 20, 15, 10];Then the output should be −const output = [40, 20, 30];ExampleFollowing ... Read More

Alternatively merging two arrays - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 13:40:43

776 Views

We are required to write a JavaScript function that takes in two arrays and merges the arrays taking elements alternatively from the arrays.For example −If the two arrays are −const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3];Then the ... Read More

Figuring out the highest value through a for in loop - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 13:39:23

118 Views

Suppose, we have a comma separator string that contains some fruit names like this −const str = 'Banana, Banana, Pear, Orange, Apple, Melon, Grape, Apple, Banana, Grape, Melon, Grape, Melon, Apple, Grape, Banana, Orange, Melon, Orange, Banana, Banana, Orange, Pear, Grape, Orange, Orange, Apple, Apple, Banana';We are required to write ... Read More

Creating an array using a string which contains the key and the value of the properties - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 13:37:51

94 Views

Suppose, we have a special kind of string like this −const str ="Integer, 1 Float, 2.0Boolean, True Integer, 6Float, 3.66 Boolean, False";We are required to write a JavaScript function that converts the above string into the following array, using the String.prototype.split() method −const arr = [    {     ... Read More

Finding reversed index of elements in arrays - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 13:36:40

1K+ Views

We are required to write a JavaScript function that takes in an array of String/Number literals as the first argument and a String/Number as the second argument.If the variable taken as the second argument is not present in the array, we should return -1.Else if the number is present in ... Read More

Sum of all prime numbers in an array - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 13:32:29

1K+ Views

We are required to write a JavaScript function that takes in an array of numbers.The function should return the sum of all the prime numbers present in the array.Let’s say the following is our array −const arr = [43, 6, 6, 5, 54, 81, 71, 56, 8, 877, 4, 4];The ... Read More

Replace backward slashes with forward slashes - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 13:31:09

890 Views

We are required to write a JavaScript function that takes in a string that may contain some backward slashes. The function should return a new string with all the backward slashes replaced with forward slashes.Let’s say the following is our string −const str = 'Th/s str/ng /conta/ns some/ forward slas/hes';ExampleFollowing ... Read More

Check if a string is sorted in JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 13:29:59

524 Views

We are required to write a JavaScript function that takes in a string and checks whether it is sorted or not.For example −isSorted('adefgjmxz')  // true isSorted('zxmfdba')     // true isSorted('dsfdsfva')     // falseExampleFollowing is the code −const str = 'abdfhlmxz'; const findDiff = (a, b) => a.charCodeAt(0) - ... Read More

Advertisements