Found 10483 Articles for Web Development

Convert number to tens, hundreds, thousands and so on - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:11:59

1K+ 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.ExampleFollowing is the code −const num = 123; const placeValue = (num, res = [], factor = 1) => {    if(num){       const val = (num % 10) * factor;       res.unshift(val);       return placeValue(Math.floor(num / 10), res, factor * 10);    };    return res; }; console.log(placeValue(num));OutputThis will produce the following output in console −[ 100, 20, 3 ]

Splitting an array based on its first value - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:10:51

353 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.Therefore, 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

Creating a two-dimensional array with given width and height in JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:08:31

706 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 subarrayval minus; the val of each element in the subarraysFor example, if the three inputs are 2, 3, 10Then the output should be −const output = [[10, 10, 10], [10, 10, 10]];ExampleFollowing is the code −const row = 2; const col = 3; const val = 10; const constructArray = (row, col, val) => {   ... Read More

How to run a function after two async functions complete - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:07:14

2K+ Views

Suppose we have an array of two elements with both of its elements being two asynchronous functions. We are required to do some work, say print something to the console (for the purpose of this question) when the execution of both the async function completes.How can we approach this challenge?There are basically two ways to perform some task upon completion of some asynchronous task −Using promisesUsing async/await functionsBut when the code includes dealing with many (more than one) asynchronous functions then the Promise.all function of the former has an edge over the latter.ExampleFollowing is the code −const arr = [ ... Read More

Compare array elements to equality - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:05:21

650 Views

We are required to write a function which compares how many values match in an array. It should be sequence dependent. That means i.e. the first object in the first array should be compared to equality to the first object in the second array and so on.For example −If the two input arrays are −const arr1 = [4, 7, 4, 3, 3, 3, 7, 6, 5]; const arr2 = [6, 5, 4, 5, 3, 2, 5, 7, 5];Then the output should be 3.We can solve this problem simply by using a for loop and checking values at the corresponding indices ... Read More

Convert a string to hierarchical object - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:03:45

431 Views

Let’s say, we have a special kind of string that contains characters in couple, like this −const str = "AABBCCDDEE";We are required to construct an object based on this string which should look like this −const obj = {    code: "AA",    sub: {        code: "BB",        sub: {            code: "CC",            sub: {                code: "DD",                sub: {                    code: "EE",     ... Read More

Remove elements from array using JavaScript filter - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:02:38

446 Views

Suppose, we have two arrays of literals like these −const arr1 = [4, 23, 7, 6, 3, 6, 4, 3, 56, 4]; const arr2 = [4, 56, 23];We are required to write a JavaScript function that takes in these two arrays and filters the first to contain only those elements that are not present in the second array.And then return the filtered array to get the below output −const output = [7, 6, 3, 6, 3];ExampleFollowing is the code −const arr1 = [4, 23, 7, 6, 3, 6, 4, 3, 56, 4]; const arr2 = [4, 56, 23]; const filterArray = (arr1, arr2) => {    const filtered = arr1.filter(el => {       return arr2.indexOf(el) === -1;    });    return filtered; }; console.log(filterArray(arr1, arr2));OutputThis will produce the following output in console −[ 7, 6, 3, 6, 3 ]

How to merge an array with an object where values are arrays - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:01:45

170 Views

Suppose, we have an array and an object like these −const arr = [1, 2, 3, 4, 5]; const obj = {    group1: ["Ram", "Mohan", "Shyam"],    group2: ["Jai", "Dinesh"], };We are required to zip the array and the object so that the values in the array are assigned to the new objects that are keyed with the values in the object.Like this −const output = {    group1: {        "Ram": 1,        "Mohan": 2,        "Shyam": 3    },    group2: {        "Jai": 4,        "Dinesh": ... Read More

Removing identical entries from an array keeping its length same - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:00:02

141 Views

We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end.For example −If we find four duplicate values, we have to remove then all and insert four empty strings at the end.ExampleFollowing is the code −const arr = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 5, 12, 1, 23, 4, 1]; const deleteAndInsert = arr => {    const creds = arr.reduce((acc, val, ind, array) => {       let { count, res } = acc;       ... Read More

Finding the maximum in a nested array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:58:04

2K+ Views

Let’s say, we have to write a simple function in JavaScript that takes in the following array of Numbers (nested to any level) −const arr = [    15, 24,    [        29, 85, 56,        [            36, 14, 6, 98, 34, 52        ],        22    ], 87, 60 ];and return the greatest number present in the array.For example, If the input array is −const arr = [    34, 65, 67,    [        43, 76, 87, 23, 56, 7,   ... Read More

Advertisements