Found 9321 Articles for Object Oriented Programming

Retrieve key and values from object in an array JavaScript

Nikitasha Shrivastava
Updated on 16-Aug-2023 16:35:56

5K+ Views

In the given problem we are required to retrieve the key and values from an object in an array with the help of Javascript. So here we will retrieve the keys and values from an object in an array with the help of two methods provided by the object class in Javascript. Understanding the Problem Statement The problem statement is to retrieve the keys and values from a given object in an array with the help of Javascript. So in Javascript the object is a collection of properties in which every property is a key and value pair. ... Read More

Partial sum in array of arrays JavaScript

Nikitasha Shrivastava
Updated on 16-Aug-2023 12:59:11

195 Views

In the given problem statement our task is to get the partial sum in an array of arrays with the help of Javascript functionalities. So we will calculate the sum of rows and give the result. Understanding the Problem The problem at hand is to compute the partial sum of an array of arrays. So first understand what an array of arrays is! Array of arrays means each item represents an array itself. For example see the below array of arrays: [ [11, 12, 13], [14, 15, 16], [17, 18, 19] ] The partial sum at ... Read More

Finding Number of Days Between Two Dates JavaScript

AmitDiwan
Updated on 25-Nov-2020 07:54:45

170 Views

We are required to write a JavaScript function that takes in two dates in the 'YYYY-MM-DD' format as the first and second argument respectively. The function should then calculate and return the number of days between the two dates.For example −If the input dates are −const str1 = '2020-05-21'; const str2 = '2020-05-25';Then the output should be −const output = 4;Exampleconst str2 = '2020-05-25'; const daysBetweenDates = (str1, str2) => {    const leapYears = (year, month) => {       if (month (year * 365) + leapYears(year, month) + monthDays[month] + d; let p = days(...str1.split('-').map(Number));   ... Read More

JavaScript Total subarrays with Sum K

Nikitasha Shrivastava
Updated on 14-Aug-2023 18:34:25

315 Views

In this problem statement, our task is to write the function for getting the total subarrays with sum K with the help of Javascript. So for doing this task we will use basic functionality of Javascript. Understanding the problem statement The problem statement is to create a function which will take an array of integers and a target sum K. So after processing the calculation it will return the total number of subarrays in the array with a sum of K. A subarray can be defined as a continuous series of items in the array. For example suppose ... Read More

Lucky Numbers in a Matrix in JavaScript

AmitDiwan
Updated on 25-Nov-2020 07:01:27

544 Views

Given a m * n matrix of distinct numbers, we have to return all lucky numbers in the 2-D array (matrix) in any order.A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.For example − If the input array is −const arr = [    [3, 7, 8],    [9, 11, 13],    [15, 16, 17] ];Then the output should be −const output = [15];because 15 is the only lucky number since it is the minimum in its row and the maximum in its column.ExampleThe code ... Read More

JavaScript - find distance between items on array

AmitDiwan
Updated on 25-Nov-2020 06:49:52

249 Views

Suppose, we have a sorted (increasing order) array of Numbers like this −const arr = [2, 5, 7, 8, 9];We are required to write a JavaScript function that takes in one such array. The function should construct a new subarray for each element of the input array.The sub-array should contain the difference (difference between that very element and the succeeding elements one by one) elements.Therefore, for the first array element, the differences are −5 - 2 = 3 7 - 2 = 5 8 - 2 = 6 9 - 2 = 7Therefore, the subarray for the first element should ... Read More

Adding a unique id for each entry in JSON object in JavaScript

Nikitasha Shrivastava
Updated on 18-May-2023 10:46:42

3K+ Views

In this problem statement, our task is to add a unique id for every object entry in a JSON object with the help of Javascript. So for doing this task we will use a loop and a variable to store the id for each object in the JSON object. Understanding the problem statement The problem statement is to write a function in Javascript by which we can add a unique id to every item in the given JSON object. For updating every JSON object by adding new id to every object we will use basic Javascript functionality. For example we ... Read More

Sorting odd and even elements separately JavaScript

Nikitasha Shrivastava
Updated on 18-May-2023 15:56:32

697 Views

In our problem statement we have to sort odd and even elements separately with the help of Javascript functionalities. So for doing this task we will use for loops and bubble sort for sorting odd and even numbers separately. Understanding the problem statement The problem statement is to write a Javascript function that will help to sort odd and even numbers in a given array. For example if we have an array [2, 3, 5, 4] then we will first sort even indexed elements [3, 4] and then sort the odd indexed elements in the array [2, 5]. After sorting ... Read More

Finding number of spaces in a string JavaScript

AmitDiwan
Updated on 25-Nov-2020 05:23:36

906 Views

We are required to write a JavaScript function that takes in a string containing spaces. The function should simply count the number of spaces present in that string.For example −If the input string is −const str = 'this is a string';Then the output should be −const output = 4;Exampleconst str = 'this is a string'; const countSpaces = (str = '') => {    let count = 0;    for(let i = 0;    i < str.length; i++){       const el = str[i];       if(el !== ' '){          continue; };          count++; };       return count; }; console.log(countSpaces(str));OutputThis will produce the following output −4

Sorting array of Number by increasing frequency JavaScript

AmitDiwan
Updated on 25-Nov-2020 05:14:30

1K+ Views

We are required to write a JavaScript function that takes in an array of numbers that might contain some repeating numbers.The function should sort the array such that the elements that are repeated for the least number of times appears first followed by the elements with increasing frequency.For example −If the input array is −const arr = [1, 1, 2, 2, 2, 3];Then the sorted array should be −const output = [3, 1, 1, 2, 2, 2];Exampleconst arr = [1, 1, 2, 2, 2, 3]; const frequencySort = (arr = []) => {    let map = {};    for ... Read More

Advertisements