Found 9321 Articles for Object Oriented Programming

Sum JavaScript arrays repeated value

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

214 Views

Suppose, we have an array of objects like this −const arr = [ {'TR-01':1}, {'TR-02':3}, {'TR-01':3}, {'TR-02':5}];We are required to write a JavaScript function that takes in one such array and sums the value of all identical keys together.Therefore, the summed array should look like −const output = [ {'TR-01':4}, {'TR-02':8}];ExampleThe code for this will be −const arr = [ {'TR-01':1}, {'TR-02':3}, {'TR-01':3}, {'TR-02':5}]; const sumDuplicate = arr => {    const map = {};    for(let i = 0; i < arr.length; ){       const key = Object.keys(arr[i])[0];       if(!map.hasOwnProperty(key)){          map[key] ... Read More

Sorting an array by date in JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:05:14

4K+ Views

Suppose, we have an array of objects like this −const arr = [{id: 1, date: 'Mar 12 2012 10:00:00 AM'}, {id: 2, date: 'Mar 8 2012 08:00:00 AM'}];We are required to write a JavaScript function that takes in one such array and sorts the array according to the date property of each object.(Either newest first or oldest first).The approach should be to convert these into JS Date Object and compare their timestamps to sort the array.ExampleThe code for this will be −const arr = [{id: 1, date: 'Mar 12 2012 10:00:00 AM'}, {id: 2, date: 'Mar 8 2012 08:00:00 AM'}]; ... Read More

Converting numbers to Indian currency using JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:03:50

2K+ Views

Suppose we have any number and are required to write a JavaScript function that takes in a number and returns its Indian currency equivalent.toCurrency(1000) --> ₹4,000.00 toCurrency(129943) --> ₹1,49,419.00 toCurrency(76768798) --> ₹9,23,41,894.00ExampleThe code for this will be −const num1 = 1000; const num2 = 129943; const num3 = 76768798; const toIndianCurrency = (num) => {    const curr = num.toLocaleString('en-IN', {       style: 'currency',       currency: 'INR'    }); return curr; }; console.log(toIndianCurrency(num1)); console.log(toIndianCurrency(num2)); console.log(toIndianCurrency(num3));OutputAnd the output in the console will be −₹1,000.00 ₹1,29,943.00 ₹7,67,68,798.00

Calculating the LCM of multiple numbers in JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:02:27

838 Views

We are required to write a JavaScript function that takes in an array of numbers of any length and returns their LCM.We will approach this problem in parts −Part 1 − We will create a helper function to calculate the Greatest Common Divisor (GCD) of two numbersPart 2 − Then using Part 1 helper function we will create another helper function to calculate the Least Common Multiple (LCM) of two numbers.Part 3 − Finally, using Part 2 helper function we will create a function that loops over the array and calculates the array LCM.ExampleThe code for this will be −const ... Read More

How to form a two-dimensional array with given width (columns) and height (rows) in JavaScript ?

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

345 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

How to turn a JSON object into a JavaScript array in JavaScript ?

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

462 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' ]

Finding the smallest value in a JSON object in JavaScript

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

640 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

Dividing a floating number, rounding it to 2 decimals, and calculating the remainder in JavaScript

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

662 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

How to make Ulam number sequence in JavaScript?

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

93 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

165 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

Advertisements