Found 9317 Articles for Object Oriented Programming

Partially reversing an array - JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:29:00

347 Views

Suppose, we have an array of literals like this −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9];We are required to write a JavaScript function that takes in such an array and a number, say n (n must be less than or equal to the length of array). And the function should reverse the first n elements of the array within.For example −If for this array, the number is 4 −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9];Then the output should be −const output = [2, 5, 5, 3, 23, 4, ... Read More

Maximum Possible Sum of Products in JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:27:49

152 Views

We are given two arrays say, arr1 and arr2 of positive Numbers. The number of values in both the arrays are the same.We are required to write a function that finds the maximum sum of products of their elements.Each element in arr1 has to be multiplied with exactly one element in arr2 and vice versa such that each element of both the arrays appears exactly once and the sum of product produced is maximum.For example: if, arr1 = [5, 1, 3, 4, 2] and, arr2 = [8, 10, 9, 7, 6]Then a possible sum of product is −5*6 + 1*7 ... Read More

Middle of three elements - JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:26:45

261 Views

We are required to write a JavaScript function that takes in three unsorted numbers and returns the middlemost of them using minimum number of comparisons.For example: If the numbers are −34, 45, 12Then our function should return the following −34ExampleFollowing is the code −const num1 = 34; const num2 = 45; const num3 = 12; const middleOfThree = (a, b, c) => {    // x is positive if a is greater than b.    // x is negative if b is greater than a.    x = a - b;    y = b - c;    z = ... Read More

Adjacent elements of array whose sum is closest to 0 - JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:25:27

112 Views

We are required to write a JavaScript function that takes in an array of numbers and returns a subarray of two elements from the original array whose sum is closest to 0.If the length of the array is less than 2, we should return the whole array.For example: If the input array is −const arr = [4, 4, 12, 3, 3, 1, 5, -4, 2, 2];Here, the sum of pair [5, -4] is 1 which is closest 0 for any two adjacent elements of the array, so we should return [5, -4]ExampleFollowing is the code −const arr = [4, 4, ... Read More

Left right subarray sum product - JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:23:49

100 Views

We are required to write a JavaScript function that takes in an array of numbers of length N (N should be even) and divides the array into two sub-array (left and right) containing N/2 elements each and do the sum of the subarrays and then multiply both the subarrays.For example: If the input array is −const arr = [1, 2, 3, 4]Then the output should be −(2+1) * (3+4) = 21ExampleFollowing is the code −const arr = [1, 2, 3, 4] const subArrayProduct = arr => {    const { length: l } = arr;    const creds = arr.reduce((acc, ... Read More

Sum of distinct elements of an array - JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:22:18

377 Views

We are required to write a JavaScript function that takes in one such array and counts the sum of all distinct elements of the array.For example: Suppose, we have an array of numbers like this −const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1];The output for the array mentioned above will be 20.ExampleFollowing is the code −const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1]; const distinctSum = arr => {    let res = 0;    for(let i = 0; i < arr.length; i++){       if(i === arr.lastIndexOf(arr[i])){          res += arr[i];       };       continue;    };    return res; }; console.log(distinctSum(arr));OutputFollowing is the output in the console −30

Check if number falls in Fibonacci series or not - JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:21:01

107 Views

We are required to write a JavaScript function that takes in a number and checks whether it falls in Fibonacci series or not. We should return a boolean.Following is the code to check for Fibonacci −Exampleconst num = 89; const isFib = query => {    if(query === 0 || query === 1){       return true;    }    let prev = 1;    let count = 2;    let temp = 0;    while(count

Find the element that appears once in sorted array - JavaScript

AmitDiwan
Updated on 15-Sep-2020 12:01:35

451 Views

Suppose, we have a sorted array of literals like this −const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9];We are required to write a JavaScript function that takes in one such array and returns the first number that appears only once in the array. If there is no such number in the array, we should return false.For this array, the output should be 6ExampleFollowing is the code −const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9]; const firstNonDuplicate = arr => {    let appeared = false;    for(let i ... Read More

Check three consecutive numbers - JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:59:47

335 Views

We are required to write a JavaScript function that takes in a Number, say n, and we are required to check whether there exist such three consecutive natural numbers (not decimal/floating point) whose sum equals to n.If there exist such numbers, our function should return them, otherwise it should return false. Following is the code −Exampleconst sum = 54; const threeConsecutiveSum = sum => {    if(sum < 6 || sum % 3 !== 0){       return false;    }    // three numbers will be of the form:    // x + x + 1 + x ... Read More

Sorting string in reverse order in JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:58:17

255 Views

We are required to write a JavaScript function that takes in a lowercase string and sorts it in the reverse order i.e., b should come before a, c before b and so on.For example: If the input string is −const str = "hello";Then the output should be −const output = "ollhe";ExampleFollowing is the code −const string = 'hello'; const sorter = (a, b) => {    const legend = [-1, 0, 1];    return legend[+(a < b)]; } const reverseSort = str => {    const strArr = str.split("");    return strArr    .sort(sorter)    .join(""); }; console.log(reverseSort(string));OutputFollowing is the output in the console −ollhe

Advertisements