Found 9321 Articles for Object Oriented Programming

Generate n random numbers between a range and pick the greatest in JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:22:46

235 Views

We are required to write a JavaScript function that takes in an array of two numbers as the first argument, this array specifies a number range within which we can generate random numbers.The second argument will be a single number that specifies the count of random numbers we have to generate.Then at last our function should return the greatest all random numbers generated.ExampleThe code for this will be −const range = [15, 26]; const count = 10; const randomBetweenRange = ([min, max]) => {    const random = Math.random() * (max - min) + min;    return random; }; const ... Read More

Function to add up all the natural numbers from 1 to num in JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:21:45

216 Views

We are required to write a JavaScript function that takes in a number, say num.Then our function should return the sum of all the natural numbers between 1 and num, including 1 and num.For example, if num is −const num = 5;Then the output should be −const output = 15;because, 1+2+3+4+5 = 15We will use the below formula to solve this problem −Sum of all natural number upto n =((n*(n+1))/2)ExampleThe code for this will be −const num = 5; const sumUpto = num => {    const res = (num * (num + 1)) / 2;    return res; }; ... Read More

Randomly shuffling an array of literals in JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:20:07

81 Views

We are required to write a JavaScript function that takes in an array of literals.Then the function should shuffle the order of elements in any random order inplace.ExampleThe code for this will be −const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; const unorderArray = arr => {    let i, pos, temp;    for (i = 0; i < 100; i++) {       pos = Math.random() * arr.length | 0;       temp = arr[pos];       arr.splice(pos, 1);       arr.push(temp);    }; } unorderArray(letters); console.log(letters);OutputAnd the output in the console will ... Read More

Search a complex object by id property in JavaScript

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

323 Views

Suppose we have a complex JSON Object like this −const obj = {    "id": "0001",    "fieldName": "sample1",    "fieldValue" "0001",    "subList": [       {          "id": 1001,          "fieldName": "Sample Child 1",          "fieldValue": "1001",          "subList": []       },       {          "id": 1002,          "fieldName": "Sample Child 2",          "fieldValue": "1002",          "subList": []       }    ] }We are required to write ... Read More

Replace alphabets with nth forward alphabet in JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:15:49

364 Views

We are required to write a JavaScript function that takes in an alphabet string and a number, say n. We should then return a new string in which all the characters are replaced by respective alphabets at position n alphabets next to them.For example, if the string and the number are −const str = 'abcd'; const n = 2;Then the output should be −const output = 'cdef';ExampleThe code for this will be −const str = 'abcd'; const n = 2; const replaceNth = (str, n) => {    const alphabet = 'abcdefghijklmnopqrstuvwxyz';    let i, pos, res = '';   ... Read More

Sort nested array containing objects ascending and descending according to date in JavaScript

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

2K+ Views

Suppose we have a JSON Object that contains a nested array like this −const arr = {    "DATA": [       {          "BookingID": "9513",          "DutyStart": "2016-02-11 12:00:00"       },       {          "BookingID": "91157307",          "DutyStart": "2016-02-11 13:00:00"       },       {          "BookingID": "95117317",          "DutyStart": "2016-02-11 13:30:00"       },       {          "BookingID": "957266",          "DutyStart": "2016-02-12 19:15:00" ... Read More

Converting array of objects to an object of objects in JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:12:22

2K+ Views

Suppose we have an array of objects like this −const arr = [{id:1, name:"aa"}, {id:2, name:"bb"}, {id:3, name:"cc"}];We are required to write a JavaScript function that takes in one such array and returns an object of the object where the key of each object should be the id property.Therefore, the output should look like this −const output = {1:{name:"aa"}, 2:{name:"bb"}, 3:{name:"cc"}};Notice that the id property is used to map the sub-objects is deleted from the sub-objects themselves.ExampleThe code for this will be −const arr = [{id:1, name:"aa"}, {id:2, name:"bb"}, {id:3, name:"cc"}]; const arrayToObject = arr => {    const res ... Read More

Comparing array elements keeping count in mind in JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:10:54

101 Views

Suppose, we have two arrays of literals that contain the same number of elements. We are supposed to write a function that checks whether or not the both arrays contain the same elements appearing for the same number of times.If the arrays fulfil this condition, we return true, false otherwise.We will create a copy of the second array, and start iterating over the first array. As we iterate, we will keep deleting the elements from the second array that are present in first array. If during iteration we encounter any element that isn't present in second array, we return false. ... Read More

Compare arrays using Array.prototype.every() in JavaScript

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

135 Views

We are required to write a JavaScript function that takes in two arrays of literals. Then our function should return true if all the elements of first array are included in the second array, irrespective of their count, false otherwise.We have to use Array.prototype.every() method to make these comparisons.ExampleThe code for this will be −const arr1 = [0, 2, 2, 2, 1]; const arr2 = [0, 2, 2, 2, 3]; const compareArrays = (arr1, arr2) => {    const areEqual = arr1.every(el => {       return arr2.includes(el);    });    return areEqual; }; console.log(compareArrays(arr1, arr2));OutputAnd the output in the console will be −false

Finding average in mixed data type array in JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:08:26

292 Views

Suppose, we have an array of mixed data types like this −const arr = [1, 2, 3, 4, 5, "4", "12", "2", 6, 7, "4", 3, "2"];We are required to write a JavaScript function that takes in one such array and returns the average of all such elements that are a number or can be partially or fully converted to a number.The string "3454fdf", isn't included in the problem array, but if it wasn’t there, we would have used the number 3454 as its contribution to average.ExampleThe code for this will be −const arr = [1, 2, 3, 4, 5, ... Read More

Advertisements