Found 9321 Articles for Object Oriented Programming

Sort array based on min and max date in JavaScript?

AmitDiwan
Updated on 23-Nov-2020 07:03:07

506 Views

Suppose, we have an array of string dates like this −const arr = [    "2017-01-22 00:21:17.0",    "2017-01-27 11:30:23.0",    "2017-01-24 15:53:21.0",    "2017-01-27 11:34:18.0",    "2017-01-26 16:55:48.0",    "2017-01-22 11:57:12.0",    "2017-01-27 11:35:43.0" ];We are required to write a JavaScript function that takes in one such array. The function should find the oldest and the newest date from this array.And then the function should finally return an object containing those two dates.Exampleconst arr = [    "2017-01-22 00:21:17.0",    "2017-01-27 11:30:23.0",    "2017-01-24 15:53:21.0",    "2017-01-27 11:34:18.0",    "2017-01-26 16:55:48.0",    "2017-01-22 11:57:12.0",    "2017-01-27 11:35:43.0" ]; const ... Read More

Group a JavaScript Array

AmitDiwan
Updated on 23-Nov-2020 07:01:10

262 Views

Suppose, we have a JavaScript array like this −const data = [    {       "dataId": "1",       "tableName": "table1",       "column": "firstHeader",       "rows": [          "a", "b", "c"       ]    },    {       "dataId": "2",       "tableName": "table1",       "column": "secondHeader",       "rows": [          "d", "e", "f",       ]    }, {       "dataId": "3",       "tableName": "table2",       "column": "aNewFirstHeader",     ... Read More

How to transform two or more spaces in a string in only one space? JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:58:05

213 Views

We have to write a JavaScript program that takes a variable user string through an input in HTML. Then through JavaScript the program should check for more than one consecutive spaces in the string.And the program should replace all such instances of more than one consecutive spaces with only one space.We can use a regular expression as the first parameter of replace. /\s{2, }/g to achieve the desired results. Let us write the code for this function −Example Live Demo REMOVE SPACES    function removeSpaces() {       var textInput = insertText.value;   ... Read More

Sorting only a part of an array JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:56:13

651 Views

We are required to write a JavaScript function that takes in an array of strings as the first argument and two numbers as second and third argument respectively.The purpose of our function is to sort the array. But we have to sort only that part of the array that falls between the start and end indices specified by second and third argument. Keeping all the other elements unchanged.For example −const arr = ['z', 'b', 'a']; sortBetween(arr, 0, 1);This function should sort the elements at 0 and 1 index only. And the array should become −const output = ['b', 'z', 'a'];Exampleconst ... Read More

Grade book challenge JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:55:03

254 Views

We are required to write a function that finds the mean of the three scores passed to it and returns the letter value associated with that grade according to the following table.Exampleconst findGrade = (...scores) => {    const {       length    } = scores;    const sum = scores.reduce((acc, val) => acc + val);    const score = sum / length;    if (score >= 90 && score = 80 ) {       return 'B';    } else if (score >= 70 ) {       return 'C';    } else if (score >= 60) {       return 'D';    } else{       return 'F';    }; } console.log(findGrade(5,40,93)); console.log(findGrade(30,85,96)); console.log(findGrade(92,70,40));OutputAnd the output in the console will be −F C D

JavaScript Count the number of unique elements in an array of objects by an object property?

AmitDiwan
Updated on 23-Nov-2020 06:51:52

638 Views

Suppose, we have the following array of objects that contains data about orders placed in a restaurant −const orders = [    {table_id: 3, food_id: 5},    {table_id: 4, food_id: 2},    {table_id: 1, food_id: 6},    {table_id: 3, food_id: 4},    {table_id: 4, food_id: 6}, ];We are required to write a JavaScript function that takes in one such array. Our function should count the number of unique table_id property in the array (i.e., the number of unique tables for which the orders are booked).And the number of unique food_id property (i.e., the number of unique food dishes ordered.)Exampleconst orders ... Read More

How to sort mixed numeric/alphanumeric array in JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:50:10

2K+ Views

Suppose we have an array of alphanumeric strings like this −const arr = ['A1', 'A10', 'A11', 'A12', 'A3A', 'A3B', 'A3', 'A4', 'B10', 'B2', 'F1', '1', '2', 'F3'];We are required to write a JavaScript function that in one such array as one and the only argument.And the function should sort this array inplace −The strings that contains only numbers should come first sorted in increasing order.The strings containing a combination of alphabets and numbers should be sorted first according to alphabets and then according to numbers in increasing order.Therefore, the output should look like −const output = ['1', '2', 'A1', 'A2', ... Read More

Compare two arrays of single characters and return the difference? JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:48:17

395 Views

We are required to compare, and get the difference, between two arrays containing single character strings appearing multiple times in each array.Example of two such arrays are −const arr1 = ['A', 'C', 'A', 'D']; const arr2 = ['F', 'A', 'T', 'T'];We will check each character at the same position and return only the parts who are different.Exampleconst arr1 = ['A', 'C', 'A', 'D']; const arr2 = ['F', 'A', 'T', 'T']; const findDifference = (arr1, arr2) => {    const min = Math.min(arr1.length, arr2.length);    let i = 0;    const res = [];    while (i < min) {   ... Read More

Finding the index position of an array inside an array JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:46:21

191 Views

Suppose, we have an array of arrays like this −const arr = [    [1, 0],    [0, 1],    [0, 0] ];We are required to write a JavaScript function that takes in one such array as the first argument and an array of exactly two Numbers as the second argument.Our function should check whether or not the array given by second input exists in the original array of arrays or not.Exampleconst arr = [ [1, 0], [0, 1], [0, 0] ];  const sub = [0, 0]; const matchEvery = (arr, ind, sub) => arr[ind].every((el, i) => el == sub[i]); ... Read More

Get range of months from array based on another array JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:44:50

324 Views

Suppose we have two arrays of strings. The first array contains exactly 12 strings, one for each month of the year like this −const year = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];The second array, contains exactly two strings, denoting a range of months like this −const monthsRange = ["aug", "oct"];We are required to write a JavaScript function that takes in two such arrays. Then the function should pick all the months from the first array that falls in the range specified by the second range arrays.Like for the above arrays, the output should be ... Read More

Advertisements