Found 9321 Articles for Object Oriented Programming

Sort array according to the date property of the objects JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:19:37

96 Views

We are required to write a JavaScript function that takes in an array of objects of dates like this −const arr = [    {date: "2016-06-08 18:10:00"},    {date: "2016-04-26 20:01:00"},    {date: "2017-02-06 14:38:00"},    {date: "2017-01-18 17:30:21"},    {date: "2017-01-18 17:24:00"} ];We are required to write a JavaScript function that takes in one such array. The function should then sort the array according to the date property of the objects.Exampleconst arr = [    {date: "2016-06-08 18:10:00"},    {date: "2016-04-26 20:01:00"},    {date: "2017-02-06 14:38:00"},    {date: "2017-01-18 17:30:21"},    {date: "2017-01-18 17:24:00"} ]; const sortByTime = (arr ... Read More

JavaScript Sum of two objects with same properties

AmitDiwan
Updated on 21-Nov-2020 06:17:52

2K+ Views

Suppose, we have two objects like these −const obj1 = {    a:12, b:8, c:17 }; const obj2 = {    a:2, b:4, c:1 };We are required to write a JavaScript function that takes in two such object.The function should sum the values of identical properties into a single property. Therefore, the final object should look something like this −const output = {    a:14, b:12, c:18 };Note − For the sake of simplicity, we have just used two objects, but we are required to write our function such that it can take in any number of objects and add ... Read More

Merge JSON array date based JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:16:25

591 Views

Suppose, we have the following array of objects −const arr = [    {       "date" : "2010-01-01",       "price" : 30    },    {       "date" : "2010-02-01",       "price" : 40    },    {       "date" : "2010-03-01",       "price" : 50    },    {       "date" : "2010-01-01",       "price2" : 45    },    {       "date" : "2010-05-01",       "price2" : 40    },    {       "date" : "2010-10-01", ... Read More

Merge JavaScript objects with the same key value and count them

AmitDiwan
Updated on 21-Nov-2020 06:13:05

4K+ Views

Suppose, we have an array of objects like this −const arr = [{    "value": 10,    "id": "111",    "name": "BlackCat", }, {    "value": 10,    "id": "111",    "name":    "BlackCat", }, {    "value": 15,    "id": "777",    "name": "WhiteCat", }];We are required to write a JavaScript function that takes in one such array.The function should then merge all those objects together that have the common value for "id" property.Therefore, for the above array, the output should look like −const output = [{    "value": 10,    "id": "111",    "name": "BlackCat",    "count": 2, ... Read More

Is the second string a rotated version of the first string JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:11:27

108 Views

We are required to write a JavaScript function that takes in two strings, say str1 and str2. We are required to determine whether or not the second string is a rotated version of the first string.For example− If the input strings are −const str1 = 'abcde'; const str2 = 'cdeab';Then the output should be true because str2 is indeed made by shifting 'ab' to the end of string in str1.Exampleconst str1 = 'abcde'; const str2 = 'cdeab'; const isRotated = (str1, str2) => {    if(str1.length !== str2.length){       return false    };    if( (str1.length || str2.length) ... Read More

Implementing binary search in JavaScript to return the index if the searched number exist

AmitDiwan
Updated on 21-Nov-2020 06:09:41

530 Views

We are required to write a JavaScript function that takes in a sorted array of numbers as the first argument and a search number as the second argument.If the search number exists in the array, we need to return its index in the array, otherwise we need to return -1.We have to do this making use of the binary search algorithm. The binary search algorithm is basically a divide and conquer algorithm which recursive divides the array into halves until it converses to a singleton element.The sorting of array is necessary of binary search algorithm in this case, as it ... Read More

Reversing words within a string JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:08:24

337 Views

We are required to write a JavaScript function that takes in a string that might contain spaces. Our function should first split the string based on the spaces and then reverse and join and return the new string.For example − If the input string is −const str = 'this is a word';Then the output should be −const output = 'siht si a drow';Exampleconst str = 'this is a word'; const reverseWords = (str = '') => {    const strArr = str.split(' ');    for(let i = 0; i < strArr.length; i++){       let el = strArr[i];   ... Read More

Is uppercase used correctly JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:06:58

29 Views

For the purpose of this very problem, we define the correct use of uppercase letter by the following rules −All letters in a word are capitals, like "INDIA".All letters in a word are not capitals, like "example".Only the first letter in a word is capital, like "Ramesh".We are required to write a JavaScript function that takes in a string determines whether or not the string complies with any of these three rules.If it does then we return true, false otherwise.Exampleconst detectCapitalUse = (word = '') => {    let allCap = true;    for (let i = 0; i < ... Read More

Go through an array and sum only numbers JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:04:59

308 Views

We are required to write a JavaScript function that takes in an array. The array might contain any type of value, Number literals, string literals, objects, undefined.Our function should pick all the Number type values and return their sumExampleconst arr = [1, 2, 'a', 4]; const countNumbers = (arr = []) => {    let sum = 0;    for(let i = 0; i < arr.length; i++){       const el = arr[i]; if(+el){          sum += +el;       };    };    return sum; } console.log(countNumbers(arr));OutputAnd the output in the console will be −7

Sort in multi-dimensional arrays in JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:03:57

546 Views

Suppose, we have the following array of arrays −const arr = [ ["A", "F", "A", "H", "F", "F"],  ["F", "A", "A", "F", "F", "H"] ];We are required to write a JavaScript function that takes in one such array.The function should sort all the subarrays of the given array internally according to these rules −If the elements are not either "A" or "F", they should maintain their positionIf the element is either of "A" or "F", they should be sorted alphabeticallyTherefore, the final output for the above array should look like −const output = [ ["A", "A", "A", "H", "A", "F"], ... Read More

Advertisements