Found 6684 Articles for Javascript

Outputting a random number that falls in a certain range in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:00:13

80 Views

We are required to write a JavaScript function that takes in a number, say n, and an array of two numbers that represents a range. The function should return an array of n random elements all lying between the range provided by the second argument.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 10; const range = [5, 15]; const randomBetween = (a, b) => {    return ((Math.random() * (b - a)) + a).toFixed(2); }; const randomBetweenRange = (num, range) => {    const res = [];    for(let i = ... Read More

Constructing an object from repetitive numeral string in JavaScript

AmitDiwan
Updated on 21-Oct-2020 11:58:45

81 Views

Suppose, we have a string with digits like this −const str = '11222233344444445666';We are required to write a JavaScript function that takes in this string and returns an object that represents the count of each number in the string.So, for this string, the output should be −const output = {    "1": 2,    "2": 4,    "3": 3,    "4": 7,    "5": 1,    "6": 3 };Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = '11222233344444445666'; const mapString = str => {    const map = {};    for(let i ... Read More

Summing up unique array values in JavaScript

AmitDiwan
Updated on 21-Oct-2020 11:57:06

105 Views

We are required to write a JavaScript function that takes in an array of numbers that may contain some duplicate numbers. Our function should return the sum of all the unique elements (elements that only appear once in the array) present in the array.For exampleIf the input array is −const arr = [2, 5, 5, 3, 2, 7, 4, 9, 9, 11];Then the output should be 25.We will simply use a for loop, iterate the array and return the sum of unique elements.ExampleThe code for this will be −const arr = [2, 5, 5, 3, 2, 7, 4, 9, 9, ... Read More

Add matching object values in JavaScript

AmitDiwan
Updated on 21-Oct-2020 11:55:11

307 Views

Suppose, we have an array of objects like this −const arr = [{a: 2, b: 5, c: 6}, {a:3, b: 4, d:1}, {a: 1, d: 2}];Each object is bound to have unique in itself (for it to be a valid object), but two different objects can have the common keys (for the purpose of this question).We are required to write a JavaScript function that takes in one such array and returns an object with all the unique keys present in the array and their values cumulative sum as the value.So, the resultant object should look like −const output = {a: ... Read More

Sorting a JSON object in JavaScript

AmitDiwan
Updated on 21-Oct-2020 11:53:46

3K+ Views

Suppose we have an object like this −const obj = {    key1: 56,    key2: 67,    key3: 23,    key4: 11,    key5: 88 };We are required to write a JavaScript function that takes in this object and returns a sorted array like this −const arr = [11, 23, 56, 67, 88];Here, we sorted the object values and placed them in an array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const obj = {    key1: 56,    key2: 67,    key3: 23,    key4: 11,    key5: 88 }; const sortObject ... Read More

Filtering array to contain palindrome elements in JavaScript

AmitDiwan
Updated on 21-Oct-2020 11:52:20

224 Views

We are required to write a JavaScript function that takes in an array of String / Number literals and returns a subarray of all the elements that were palindrome in the original array.For exampleIf the input array is −const arr = ['carecar', 1344, 12321, 'did', 'cannot'];Then the output should be −const output = [12321, 'did'];We will create a helper function that takes in a number or a string and checks if its a boolean or not.Then we will loop over the array, filter the palindrome elements and return the filtered array.Therefore, let’s write the code for this function −ExampleThe code ... Read More

Reverse mapping an object in JavaScript

AmitDiwan
Updated on 20-Oct-2020 12:19:45

793 Views

Suppose, we have an object like this −const products = {    "Pineapple":38,    "Apple":110,    "Pear":109 };All the keys are unique in themselves and all the values are unique in themselves.We are required to write a function that accepts a value and returns its key. Let' say we have created a function findKey().For example, findKey(110) should return "Apple".We will approach this problem by first reverse mapping the values to keys and then simply using object notations to find their values.Therefore, let’s write the code for this function −ExampleThe code for this will be −const products = {    "Pineapple":38, ... Read More

Expanding Numerals in JavaScript

AmitDiwan
Updated on 20-Oct-2020 12:17:49

81 Views

We are required to write a function that, given a number, say, 123, will output an array −[100, 20, 3]Basically, the function is expected to return an array that contains the place value of all the digits present in the number taken as an argument by the function.We can solve this problem by using a recursive approach.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 123; const placeValue = (num, res = [], factor = 1) => {    if(num){       const val = (num % 10) * factor;   ... Read More

Grouping array of array on the basis of elements in JavaScript

AmitDiwan
Updated on 20-Oct-2020 12:15:43

160 Views

Suppose, we have an array of arrays of numbers like this −const arr = [[1, 45], [1, 34], [1, 49], [2, 34], [4, 78], [2, 67], [4, 65]];Each subarray is bound to contain strictly two elements. We are required to write a function that constructs a new array where all second elements of the subarrays that have similar first value are grouped together.So, for the array above, the output should look like −const output = [    [45, 34, 49],    [34, 67],    [78, 65] ];We can make use of the Array.prototype.reduce() method that takes help of a Map() ... Read More

Constructing 2-D array based on some constraints in JavaScript

AmitDiwan
Updated on 20-Oct-2020 12:13:21

68 Views

We are required to write a JavaScript function that creates a multi-dimensional array based on some inputs.It should take in three elements, namely −row - the number of subarrays to be present in the array, col - the number of elements in each subarray, val - the val of each element in the subarrays, For example, if the three inputs are 2, 3, 10Then the output should be −const output = [[10, 10, 10], [10, 10, 10]];Therefore, let’s write the code for this function −ExampleThe code for this will be −const row = 2; const col = 3; const val ... Read More

Advertisements