Found 10483 Articles for Web Development

Rotate number to form the maximum number using JavaScript

AmitDiwan
Updated on 21-Apr-2021 07:00:42

164 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function is required to return the maximum value by rearranging its digits.ExampleFollowing is the code −const num = 124; const rotateToMax = n => {    n = n       .toString()       .split('')       .map(el => +el);       n.sort((a, b) =>       return b - a;    });    return n    .join(''); }; console.log(rotateToMax(num));Output421

Sorting and find sum of differences for an array using JavaScript

AmitDiwan
Updated on 21-Apr-2021 07:00:27

351 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers. Our function should sum the differences between consecutive pairs in the array in descending order.For example − If the array is −[6, 2, 15]Then the output should be −(15 - 6) + (6 - 2) = 13ExampleFollowing is the code − Live Democonst arr = [6, 2, 15]; const sumDifference = (arr = []) => {    const descArr = arr.sort((a, b) => b - a);    if (descArr.length

Is the digit divisible by the previous digit of the number in JavaScript

AmitDiwan
Updated on 21-Apr-2021 07:00:06

206 Views

ProblemWe are required to write a JavaScript function that takes in a number and checks each digit if it is divisible by the digit on its left and returns an array of booleans.The booleans should always start with false because there is no digit before the first one.ExampleFollowing is the code − Live Democonst num = 73312; const divisibleByPrevious = (n = 1) => {    const str = n.toString();    const arr = [false];    for(let i = 1; i < str.length; ++i){       if(str[i] % str[i-1] === 0){          arr.push(true);       }else{          arr.push(false);       };    };    return arr; }; console.log(divisibleByPrevious(num));Output[ false, false, true, false, true ]

Generating desired pairs within a range using JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:59:46

229 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should generate an array containing the pairs of integers [a, b] that satisfy the following conditions −0

Constructing an array of addition/subtractions relative to first array element in JavaScript

AmitDiwan
Updated on 21-Apr-2021 07:01:55

137 Views

ProblemWe are required to write a JavaScript function that takes in an array of positive integers. Our function should map this array to an array of string integers.The array should contain the number we should add/subtract to the first element to achieve the corresponding element.For example[4, 3, 6, 2]should return −['+0', '-1', '+2', '-2']ExampleFollowing is the code − Live Democonst arr = [4, 3, 6, 2]; const buildRelative = (arr = []) => {    const res = [];    let num = '';    for(let i of arr){       if(i - arr[0] >= 0){         ... Read More

Constructing an array of smaller elements than the corresponding elements based on input array in JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 10:21:19

145 Views

In the realm of JavaScript programming, the ability to construct an array of smaller elements than their corresponding counterparts from an input array holds immense significance. This intriguing technique allows developers to manipulate and transform data, opening doors to innovative solutions and efficient algorithms. By harnessing the power of JavaScript's array manipulation capabilities, developers can generate new arrays with elements that are scaled down or reduced based on the values of the original array. In this article, we delve into the art of constructing an array of smaller elements than the corresponding elements from an input array, exploring the underlying ... Read More

Finding the 1-based index of a character in alphabets using JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:58:32

206 Views

ProblemWe are required to write a JavaScript function that takes in a lowercase English alphabet character. Our function should return the character’s 1-based index in the alphabets.ExampleFollowing is the code − Live Democonst char = 'j'; const findCharIndex = (char = '') => {    const legend = ' abcdefghijklmnopqrstuvwxyz';    if(!char || !legend.includes(char) || char.length !== 1){       return -1;    };    return legend.indexOf(char); }; console.log(findCharIndex(char));Output10

Counting number of triangle sides in an array in JavaScript

AmitDiwan
Updated on 21-Apr-2021 07:06:11

219 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument.The task of our function is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.For example, if the input to the function is −const arr = [2, 2, 3, 4];Then the output should be −const output = 3;Output ExplanationValid combinations are:2, 3, 4 (using the first 2) 2, 3, 4 (using the second 2) 2, 2, 3ExampleFollowing is the code − Live Democonst arr ... Read More

Implementing a custom function like Array.prototype.filter() function in JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:52:46

912 Views

ProblemWe are required to write a JavaScript function that lives on the prototype Object of the Array class.Our function should take in a callback function as the only argument. This callback function should be called for each element of the array.And that callback function should take in two arguments the corresponding element and its index. If the callback function returns true, we should include the corresponding element in our output array otherwise we should exclude it.ExampleFollowing is the code − Live Democonst arr = [5, 3, 6, 2, 7, -4, 8, 10]; const isEven = num => num % 2 === ... Read More

Summing cubes of natural numbers within a range in JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:52:13

166 Views

ProblemWe are required to write a JavaScript function that takes in a range array of two numbers. Our function should find the sum of all the cubes of the numbers that falls in the specified range.ExampleFollowing is the code − Live Democonst range = [4, 11]; const sumCubes = ([l, h]) => {    const findCube = num => num * num * num;    let sum = 0;    for(let i = l; i

Advertisements