Form a Two-Dimensional Array in JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:00:32

405 Views

We are required to write a JavaScript function that takes in three arguments −height --> no. of rows of the array width --> no. of columns of the array val --> initial value of each element of the arrayThen the function should return the new array formed based on these criteria.ExampleThe code for this will be −const rows = 4, cols = 5, val = 'Example'; const fillArray = (width, height, value) => {    const arr = Array.apply(null, { length: height }).map(el => {       return Array.apply(null, { length: width }).map(element => {         ... Read More

Turn JSON Object into JavaScript Array

AmitDiwan
Updated on 20-Nov-2020 09:59:28

591 Views

Suppose, we have this JSON object where index keys are mapped to some literals −const obj = {    "0": "Rakesh",    "1": "Dinesh",    "2": "Mohit",    "3": "Rajan",    "4": "Ashish" };We are required to write a JavaScript function that takes in one such object and uses the object values to construct an array of literals.ExampleThe code for this will be −const obj = {    "0": "Rakesh",    "1": "Dinesh",    "2": "Mohit",    "3": "Rajan",    "4": "Ashish" }; const objectToArray = (obj) => {    const res = [];    const keys = Object.keys(obj);    keys.forEach(el => {       res[+el] = obj[el];    });    return res; }; console.log(objectToArray(obj));OutputAnd the output in the console will be −[ 'Rakesh', 'Dinesh', 'Mohit', 'Rajan', 'Ashish' ]

Find Smallest Value in a JSON Object in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:57:09

843 Views

We are required to write a JavaScript function that takes in a JSON object as one and only argument.The JSON object has string keys mapped to some numbers. Our function should traverse through the object, find and return the smallest value from the object.ExampleThe code for this will be −const obj = {    "a": 4,    "b": 2,    "c": 5,    "d": 1,    "e": 3 }; const findSmallestValue = obj => {    const smallest = Object.keys(obj).reduce((acc, val) => {       return Math.min(acc, obj[val]);    }, Infinity);    return smallest; } console.log(findSmallestValue(obj));OutputAnd the output in the console will be −1

Divide Floating Number and Round to 2 Decimals in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:56:06

829 Views

Suppose, we have a floating-point number −2.74If we divide this number by 4, the result is 0.685.We want to divide this number by 4 but the result should be rounded to 2 decimals.Therefore, the result should be −3 times 0.69 and a remainder of 0.67ExampleThe code for this will be −const num = 2.74; const parts = 4; const divideWithPrecision = (num, parts, precision = 2) => {    const quo = +(num / parts).toFixed(precision);    const remainder = +(num - quo * (parts - 1)).toFixed(precision);    if(quo === remainder){       return {          parts, ... Read More

Make Ulam Number Sequence in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:54:33

134 Views

A mathematician Ulam proposed generating a sequence of numbers from any positive integer n (n>0) as follows −If n is 1, it will stop. if n is even, the next number is n/2. if n is odd, the next number is 3 * n + 1. continue with the process until reaching 1.Here are some examples for the first few integers −2->1 3->10->5->16->8->4->2->1 4->2->1 6->3->10->5->16->8->4->2->1 7->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1We are required to write a JavaScript function that takes in a number and returns the Ulam sequence starting with that number.ExampleThe code for this will be −const num = 7; const generateUlam = num ... Read More

Read by Key and Parse as JSON in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:52:57

252 Views

Suppose, we have a JSON array like this −const arr = [{    "data": [       { "W": 1, "A1": "123" },       { "W": 1, "A1": "456" },       { "W": 2, "A1": "4578" },       { "W": 2, "A1": "2423" },       { "W": 2, "A1": "2432" },       { "W": 2, "A1": "24324" }    ] }];We are required to write a JavaScript function that takes in one such array and converts it to the following JSON array −[    {       "1": ... Read More

Find Smallest Sum of Indices of Unique Number Pairs in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:50:29

157 Views

We are required to write a function that takes in an array of numbers as the first argument and a target sum as the second argument. We then want to loop through the array and then add each value to each other (except itself + itself).And if the sum of the two values that were looped through equals the target sum, and the pair of values hasn't been encountered before, then we remember their indices and, at the end, return the full sum of all remembered indices.If the array is −const arr = [1, 4, 2, 3, 0, 5];And the ... Read More

Filtering Array of Objects in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:48:18

802 Views

Suppose, we have two arrays of literals and objects respectively −const source = [1, 2, 3 , 4 , 5]; const cities = [{ city: 4 }, { city: 6 }, { city: 8 }];We are required to write a JavaScript function that takes in these two arrays. Our function should create a new array that contains all those elements from the array of objects whose value for "city" key is present in the array of literals.ExampleLet us write the code −const source = [1, 2, 3 , 4 , 5]; const cities = [{ city: 4 }, { city: ... Read More

Compare Objects in JavaScript and Return Common Keys with Values

AmitDiwan
Updated on 20-Nov-2020 09:47:10

766 Views

We are required to write a JavaScript function that takes in two objects. The function should return an array of all those common keys that have common values across both objects.ExampleThe code for this will be −const obj1 = { a: true, b: false, c: "foo" }; const obj2 = { a: false, b: false, c: "foo" }; const compareObjects = (obj1 = {}, obj2 = {}) => {    const common = Object.keys(obj1).filter(key => {       if(obj1[key] === obj2[key] && obj2.hasOwnProperty(key)){          return true;       };       return false;    });    return common; }; console.log(compareObjects(obj1, obj2));OutputAnd the output in the console will be −['b', 'c']

Convert Square Bracket Object Keys into Nested Object in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:44:23

772 Views

We know that there are two ways we can access nested keys within an Object in JavaScript.For instance, take this object −const obj = {    object: {       foo: {          bar: {             ya: 100          }       }    } };If we needed to access or update the nested property 'ya', we can access it like −Way 1 −obj['object']['foo']['bar']['ya']orWay 2 −obj.object.foo.bar.yaBoth these ways lead us to the same destination.We are required to write a JavaScript function that takes in the path to ... Read More

Advertisements