Found 6710 Articles for Javascript

Product of two number using HOC - JavaScript

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

272 Views

HOCHOC or Higher Order Functions in JavaScript are a special type of functions that receives another function as argument or have a function set as their return value or do both. HOC along with closures is a very powerful tool in JavaScript.We are required to write a JavaScript Higher Order Function that can be used to obtain the product of two numbers.ExampleFollowing is the code −const num1 = 24; const num2 = 5; const productHOC = num1 => {    return product = num2 => {       return num1 * num2;    }; }; console.log(productHOC(num1)(num2));OutputFollowing is the output in the console −120

Removing 0s from start and end - JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:38:09

144 Views

We are required to write a JavaScript function that takes in a number as a string and returns a new number string with all the leading and trailing 0s removedFor example: If the input is −const strNum = '054954000'Then the output should be −const output = '54954'ExampleFollowing is the code −const strNum = '054954000'; const removeZero = (str = '') => {    const res = '';    let startLen = 0, endLen = str.length-1;    while(str[startLen] === '0'){       startLen++;    };    while(str[endLen] === '0'){       endLen--;    };    return str.substring(startLen, endLen+1); }; console.log(removeZero(strNum));OutputFollowing is the output in the console −54954

Finding special array - JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:36:10

542 Views

An array is a special array if −--All the elements at odd indices are odd. --All the elements at even indices are even.We are required to write a JavaScript function that takes in an array and checks if its a special array or not.ExampleFollowing is the code −const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const isSpecial = (arr = []) => {    for(let i = 0; i < arr.length; i++){       if(arr[i] % 2 === i % 2){          continue;       };       return false;    };    return true; }; console.log(isSpecial(arr));OutputFollowing is the output in the console −true

Move string capital letters to front maintaining the relative order - JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:35:03

313 Views

We are required to write a JavaScript function that takes in a string with uppercase and lowercase letters. The function should return a string with all the uppercase letters moved to front of the string.For example: If the input string is −const str = 'heLLO woRlD';Then the output should be −const output = 'LLORDhe wol';ExampleFollowing is the code −const str = 'heLLO woRlD'; const moveCapitalToFront = (str = '') => {    let capitalIndex = 0;    const newStrArr = [];    for(let i = 0; i < str.length; i++){       if(str[i] !== str[i].toLowerCase()){         ... Read More

Concatenating variable number of arrays into one - JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:33:56

542 Views

We are required to write a JavaScript function that takes in any number of JavaScript arrays and returns one single array with all the values from input arrays concatenated into it.For example − If the input arrays are −[1, 5], [44, 67, 3], [2, 5], [7], [4], [3, 7], [6]Then the output should be −const output = [1, 5, 44, 67, 3, 2, 5, 7, 4, 3, 7, 6];ExampleFollowing is the code −const a = [1, 5], b = [44, 67, 3], c = [2, 5], d = [7], e = [4], f = [3, 7], g = [6]; const concatArrays = (...arr) => {    const res = arr.reduce((acc, val) => {       return acc.concat(...val);    }, []);    return res; }; console.log(concatArrays(a, b, c, d, e, f, g));OutputFollowing is the output in the console −[    1, 5, 44, 67, 3,    2, 5,  7,  4, 3,    7, 6 ]

Array of multiples - JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:32:46

931 Views

We are required to write a JavaScript function that takes in two numbers, say m and n, and it returns an array of first n multiples of m.For example − If the numbers are 4 and 6Then the output should be −const output = [4, 8, 12, 16, 20, 24]ExampleFollowing is the code −const num1 = 4; const num2 = 6; const multiples = (num1, num2) => {    const res = [];    for(let i = num1; i

Finding tidy numbers - JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:31:44

248 Views

A tidy number is a number whose digits are in non-decreasing order. We are required to write a JavaScript function that takes in a number and checks whether its a tidy number or not.For example −489 is a tidy number 234557 is also a tidy number 34535 is not a tidy numberExampleFollowing is the code −const num = 234789; const isTidy = (num, last = 10) => {    if(num){       if(num % 10 > last){          return false;       };       return isTidy(Math.floor(num / 10), (num % 10));    };    return true; }; console.log(isTidy(num));OutputFollowing is the output in the console −true

Number of smaller and larger elements - JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:30:24

418 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 this array and a number, say n, and returns an object representing the count of elements greater than and smaller than n.ExampleFollowing is the code −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9]; const greaterSmallerNumbers = (arr, num) => {    return arr.reduce((acc, val) => {       let { greater, smaller } = acc;       if(val > num){          greater++;       };       if(val < num){          smaller++;    };    return { greater, smaller };    }, {       greater: 0,       smaller: 0    }); }; console.log(greaterSmallerNumbers(arr, 3));OutputFollowing is the output in the console −{ greater: 8, smaller: 1 }

Partially reversing an array - JavaScript

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

452 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

207 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

Advertisements