Found 9150 Articles for Object Oriented Programming

Taking common elements from many arrays in JavaScript

AmitDiwan
Updated on 22-Oct-2020 12:50:49

520 Views

We are required to write a JavaScript function that takes in any arbitrary number of arrays and returns an array of elements that are common to all arrays. If there are no common elements, then we should return an empty array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = [2, 6, 7, 1, 7, 8, 4, 3]; const arr2 = [5, ,7, 2, 2, 1, 3]; const arr3 = [1, 56, 345, 6, 54, 2, 68, 85, 3]; const intersection = (arr1, arr2) => {    const res = [];    for(let ... Read More

Summing up digits and finding nearest prime in JavaScript

AmitDiwan
Updated on 22-Oct-2020 12:49:13

111 Views

We are required to write a JavaScript function that takes in a number, finds the sum of its digits and returns a prime number that is just greater than or equal to the sum.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 56563; const digitSum = (num, sum = 0) => {    if(num){       return digitSum(Math.floor(num / 10), sum + (num % 10));    }    return sum; }; const isPrime = n => {    if (n===1){       return false;    }else if(n === 2){   ... Read More

Find the Smallest element from a string array in JavaScript

AmitDiwan
Updated on 22-Oct-2020 12:46:07

292 Views

We are required to write a JavaScript function that takes in an array of strings and returns the index of string that is shortest in length.We will simply use a for loop and persist the index of string which is shortest in length.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = ['this', 'can', 'be', 'some', 'random', 'sentence']; const findSmallest = arr => {    const creds = arr.reduce((acc, val, index) => {       let { ind, len } = acc;       if(val.length < len){         ... Read More

Adding up identical elements in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:42:29

99 Views

We are required to write a JavaScript function that takes in an array of Numbers and sums all the identical numbers together at one index.For exampleIf the input array is −const arr = [20, 10, 15, 20, 15, 10];Then the output should be −const output = [40, 20, 30];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [20, 10, 15, 20, 15, 10]; const addSimilar = arr => {    for(let i = 0; i < arr.length; i++){       while(i !== arr.lastIndexOf(arr[i])){          const ind = arr.lastIndexOf(arr[i]);          arr[i] += arr.splice(ind, 1)[0];       };    }; }; addSimilar(arr); console.log(arr);OutputThe output in the console will be −[ 40, 20, 30 ]

Merging two arrays in a unique way in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:41:09

146 Views

We are required to write a JavaScript function that takes in two arrays and merges the arrays taking elements alternatively from the arrays.For exampleIf the two arrays are −const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3];Then the output should be −const output = [4, 2, 3, 1, 2, 6, 5, 8, 6, 9, 8, 4, 9, 3];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3]; const mergeAlernatively = (arr1, arr2) => {    const res = [];    for(let i = 0; i < arr1.length + arr2.length; i++){       if(i % 2 === 0){          res.push(arr1[i/2]);       }else{          res.push(arr2[(i-1)/2]);       };    };    return res; }; console.log(mergeAlernatively(arr1, arr2));OutputThe output in the console will be −[    4, 2, 3, 1, 2, 6,    5, 8, 6, 9, 8, 4,    9, 3 ]

Picking out uniques from an array in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:37:34

128 Views

Suppose we have an array that contains duplicate elements like this −const arr = [1, 1, 2, 2, 3, 4, 4, 5];We are required to write a JavaScript function that takes in one such array and returns a new array. The array should only contain the elements that only appear once in the original array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [1, 1, 2, 2, 3, 4, 4, 5]; const extractUnique = arr => {    const res = [];    for(let i = 0; i < arr.length; i++){   ... Read More

Non-composite numbers sum in an array in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:36:18

156 Views

We are required to write a JavaScript function that takes in an array of numbers.The function should return the sum of all the prime numbers present in the array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [43, 6, 6, 5, 54, 81, 71, 56, 8, 877, 4, 4]; const isPrime = n => {    if (n===1){       return false;    }else if(n === 2){       return true;    }else{       for(let x = 2; x < n; x++){          if(n % ... Read More

Inverting slashes in a string in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:34:40

335 Views

We are required to write a JavaScript function that takes in a string that may contain some backward slashes.And the function should return a new string where all the backslashes with forward slashes.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'Th/s str/ng /conta/ns some/ forward slas/hes'; const invertSlashes = str => {    let res = '';    for(let i = 0; i < str.length; i++){       if(str[i] !== '/'){          res += str[i];          continue;       };       ... Read More

Finding sort order of string in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:33:23

176 Views

We are required to write a JavaScript function that takes in a string and checks whether it is sorted or not.For example:isSorted('adefgjmxz') // true isSorted('zxmfdba') // true isSorted('dsfdsfva') // falseTherefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'abdfhlmxz'; const findDiff = (a, b) => a.charCodeAt(0) - b.charCodeAt(0); const isStringSorted = (str = '') => {    if(str.length < 2){       return true;    };    let res = ''    for(let i = 0; i < str.length-1; i++){       if(findDiff(str[i+1], str[i]) > 0){         ... Read More

Pushing false objects to bottom in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:30:28

182 Views

Suppose we have an array of objects like this −const array = [    {key: 'a', value: false},    {key: 'a', value: 100},    {key: 'a', value: null},    {key: 'a', value: 23} ];We are required to write a JavaScript function that takes in one such array and places all the objects that have falsy values for the "value" property to the bottom and sorts all other objects in decreasing order by the "value" property.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [    {key: 'a', value: false},    {key: 'a', ... Read More

Advertisements