Found 9316 Articles for Object Oriented Programming

Filtering out only null values in JavaScript

AmitDiwan
Updated on 10-Oct-2020 08:02:46

296 Views

We are required to write a JavaScript function that takes in an array that contains some false values. The function should remove all the null values from the array (if there are any) in place.For example: If the input array is −const arr = [12, 5, undefined, null, 0, false, null, 67, undefined, false, null];Then the output should be −const output = [12, 5, undefined, 0, false, 67, undefined, false];ExampleThe code for this will be −const arr = [12, 5, undefined, null, 0, false, null, 67, undefined, false, null]; const removeNullValues = arr => {    for(let i = 0; ... Read More

Split Array by part base on N count in JavaScript

AmitDiwan
Updated on 10-Oct-2020 08:00:59

270 Views

We are required to write a JavaScript function that takes in an array of literals and a number, say n.The function should return a new array, chunked into n subarrays, given that n will always be less than or equal to the length of the array.For example: If the input array is −const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; const n = 3;Then the output should be −const output = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]];ExampleThe code for this will be −const arr = [1, 2, 3, 4, 5, 6, ... Read More

Reversing words in a string in JavaScript

AmitDiwan
Updated on 10-Oct-2020 07:59:33

1K+ Views

We are required to write a JavaScript function that takes in a string. The function should return a new string that has all the words of the original string reversed.For example, If the string is −const str = 'this is a sample string';Then the output should be −const output = 'siht si a elpmas gnirts';ExampleThe code for this will be −const str = 'this is a sample string'; const reverseWords = str => {    let reversed = '';    reversed = str.split(" ")    .map(word => {       return word       .split("")       ... Read More

Check if an array is descending, ascending or not sorted in JavaScript

AmitDiwan
Updated on 10-Oct-2020 07:57:59

937 Views

We are required to write a JavaScript function that takes in an array of Numbers. The function should check whether the numbers in the array are in increasing order, or in decreasing order or in no specific order.If the array contains only one element then we should return a message saying not enough elements.And if the array has all the elements equal, we should return a message saying all elements are equal.ExampleThe code for this will be −const arr1 = [7, 2, 1, 3, 2, 1]; const arr2 = [1, 1, 2, 3, 7, 7]; const determineOrder = arr => ... Read More

Recursively adding digits of a number in JavaScript

AmitDiwan
Updated on 10-Oct-2020 07:56:16

143 Views

We are required to write a JavaScript function that takes in a number and recursively adds the digits of the number until the result is not a single digit number.For example, If the number is −54563Then the output should be 5, because,= 5 + 4 + 5 + 6 + 3 = 23 = 2 + 3 = 5ExampleThe code for this will be −const num = 54563; const addRecursively = num => {    if(num < 10){       return num;    };    let sum = 0;    while(num !== 0) {       sum += (num%10);       num = parseInt(num/10);    };    return addRecursively(sum); }; console.log(addRecursively(num));OutputThe output in the console −3

How to convert array into array of objects using map() and reduce() in JavaScript

AmitDiwan
Updated on 10-Oct-2020 07:54:46

820 Views

Suppose we have an array of arrays like this −const arr = [    [       ['juice', 'apple'], ['maker', 'motts'], ['price', 12]    ],    [       ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11]    ] ];We are required to write a JavaScript function that takes in one such array and returns a new array of objects built based on the input array.So, for the above array, the output should look like this −const output = [    {juice: 'apple', maker: 'motts', price: 12},    {juice: 'orange', maker: 'sunkist', price: 11} ];ExampleThe code for this will be ... Read More

How to convert array of decimal strings to array of integer strings without decimal in JavaScript

AmitDiwan
Updated on 10-Oct-2020 07:52:49

563 Views

We are required to write a JavaScript function that takes in an array of decimal strings. The function should return an array of strings of integers obtained by flooring the original corresponding decimal values of the array.For example, If the input array is −const input = ["1.00","-2.5","5.33333","8.984563"];Then the output should be −const output = ["1","-2","5","8"];ExampleThe code for this will be −const input = ["1.00","-2.5","5.33333","8.984563"]; const roundIntegers = arr => {    const res = [];    arr.forEach((el, ind) => {       const strNum = String(el);       res[ind] = parseInt(strNum);    });    return res; }; console.log(roundIntegers(input));OutputThe output in the console −[ 1, -2, 5, 8 ]

Group by element in array JavaScript

AmitDiwan
Updated on 10-Oct-2020 07:50:13

340 Views

Suppose, we have an array of objects like this −const arr = [    {"name": "toto", "uuid": 1111},    {"name": "tata", "uuid": 2222},    {"name": "titi", "uuid": 1111} ];We are required to write a JavaScript function that splits the objects into separate array of arrays that have the similar values for the uuid property.OutputTherefore, the output should look like this −const output = [    [       {"name": "toto", "uuid": 1111},       {"name": "titi", "uuid": 1111}    ],    [       {"name": "tata", "uuid": 2222}    ] ];The code for this will be −const ... Read More

Find array elements that are out of order in JavaScript

AmitDiwan
Updated on 10-Oct-2020 07:48:25

188 Views

Suppose we have an array of sorted numbers but some elements of the array are out of their sorted order.We are required to write a JavaScript function that takes in one such array and returns a subarray of all those elements that are out of order.ExampleThe code for this will be −const arr = ["2", "3", "7", "4", "5", "6", "1"]; const findOutOfOrder = arr => {    let notInOrder = [];    notInOrder = arr.filter((el, ind) => {       return ind && this.next !== +el || (this.next = +el + 1, false);    }, {       next: null    });    return notInOrder; }; console.log(findOutOfOrder(arr));OutputThe output in the console −[ '7', '1' ]

Converting strings to snake case in JavaScript

AmitDiwan
Updated on 10-Oct-2020 07:46:19

589 Views

Snake case is basically a style of writing strings by replacing the spaces with '_' and converting the first letter of each word to lowercase.We are required to write a JavaScript function that takes in a string and converts it to snake case.ExampleThe code for this will be −const str = 'This is a simple sentence'; const toSnakeCase = (str = '') => {    const strArr = str.split(' ');    const snakeArr = strArr.reduce((acc, val) => {       return acc.concat(val.toLowerCase());    }, []);    return snakeArr.join('_'); }; console.log(toSnakeCase(str));OutputFollowing is the output on console −this_is_a_simple_sentence

Advertisements