Found 9316 Articles for Object Oriented Programming

Finding upper elements in array in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:24:44

99 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return an array of all the elements from the input array that are greater than or equal to the number taken as the second argument.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [56, 34, 2, 7, 76, 4, 45, 3, 3, 34, 23, 2, 56, 5]; const threshold = 40; const findGreater = (arr, num) => {    const res = ... Read More

Maps in JavaScript takes keys and values array and maps the values to the corresponding keys

AmitDiwan
Updated on 21-Oct-2020 12:22:55

122 Views

Suppose we have two arrays −const keys = [0, 4, 2, 3, 1]; const values = ["first", "second", "third", "fourth", "fifth"];We are required to write a JavaScript function that takes in the keys and the values array and maps the values to the corresponding keys.Therefore, the output should look like −const map = {    0 => 'first',    4 => 'second',    2 => 'third',    3 => 'fourth',    1 => 'fifth' };Therefore, let’s write the code for this function −ExampleThe code for this will be −const keys = [0, 4, 2, 3, 1]; const values = ["first", ... Read More

Sort the second array according to the elements of the first array in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:04:17

131 Views

Suppose, we have two arrays like these −const arr1 = ['d', 'a', 'b', 'c'] ; const arr2 = [{a:1}, {c:3}, {d:4}, {b:2}];We are required to write a JavaScript function that accepts these two arrays. The function should sort the second array according to the elements of the first array.We have to sort the keys of the second array according to the elements of the first array.Therefore, the output should look like −const output = [{d:4}, {a:1}, {b:2}, {c:3}];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = ['d', 'a', 'b', 'c'] ; const ... Read More

Absolute difference of Number arrays in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:02:46

373 Views

Suppose, we have two arrays like these −const arr1 = [1, 2, 3, 4, 5, 6]; const arr2 = [9, 8, 7, 5, 8, 3];We are required to write a JavaScript function that takes in such two arrays and returns an array of absolute difference between the corresponding elements of the array.So, for these arrays, the output should look like −const output = [8, 6, 4, 1, 3, 3];We will use a for loop and keep pushing the absolute difference iteratively into a new array and finally return the array.Therefore, let’s write the code for this function −ExampleThe code for ... Read More

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

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

79 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

223 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

Advertisements