Found 6710 Articles for Javascript

Filtering out numerals from string in JavaScript

AmitDiwan
Updated on 21-Apr-2021 12:02:33

167 Views

ProblemWe are required to write a JavaScript function that takes in a string, str, which contains a combination of alphabets, special characters and numbers.Our function should return a new string based on the input string that contains only the numbers present in the string str, maintaining their relative order.For example, if the input to the function is −const str = 'revd1fdfdfs2v34fd5gfgfd6gffg7ds';Then the output should be −const output = '1234567';ExampleFollowing is the code − Live Democonst str = 'revd1fdfdfs2v34fd5gfgfd6gffg7ds'; const pickNumbers = (str = '') => {    let res = '';    for(let i = 0; i < str.length; i++){   ... Read More

Decreasing order sort of alphabets in JavaScript

AmitDiwan
Updated on 21-Apr-2021 11:56:19

179 Views

ProblemWe are required to write a JavaScript function that takes in a lowercase English alphabets string, str, as the first and the only argumentOur function should create and return a new string based on the input string which contains characters sorted according to the reverse English alphabets.For example, if the input to the function is −const str = 'abcdef';Then the output should be −const output = 'fedcba';ExampleFollowing is the code − Live Democonst str = 'abcdef'; const reverseSorting = (str = '') => {    const strArr = str.split('');    const mapString = 'abcdefghijkmnopqrstuvwxyz';    const sorter = (a, b) => ... Read More

Merging and rectifying arrays in JavaScript

AmitDiwan
Updated on 21-Apr-2021 11:50:24

134 Views

ProblemWe are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2, as the first and the second arguments.Our function should merge the elements of both these arrays into a new array and if upon merging or before merging there exists any duplicates, we should delete the excess duplicates so that only one copy of each element is present in the merged array.The order here is not so important but the frequency of elements (which should be 1 for each element) is important.For example, if the input to the function is −onst arr1 = ... Read More

Checking for special numbers in JavaScript

Ranjit Kumar
Updated on 21-Apr-2021 11:39:20

470 Views

ProblemWe are required to write a JavaScript function that takes in a number, num, as the first and the only argument.Our function should return true if the sum of the digits of the number num is a palindrome number, false otherwise.For example, if the input to the function is −const num = 781296;Then the output should be −const output = true;Output ExplanationBecause the digit sum of 781296 is 33 which is a palindrome number.ExampleFollowing is the code − Live Democonst num = 781296; const findSum = (num, sum = 0) => { if(num){ return findSum(Math.floor(num / 10), sum + (num ... Read More

Accumulating array elements to form new array in JavaScript

AmitDiwan
Updated on 21-Apr-2021 10:32:48

235 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a number, num, (num {    const res = [];    let sum = 0, right = 0, left = 0;    for(; right < num; right++){       sum += arr[right];    };    res.push(sum);    while(right < arr.length){       sum -= arr[left];       sum += arr[right];       right++;       left++;       res.push(sum);    };    return res; }; console.log(accumulateArray(arr, num));OutputFollowing is the console output−[3, 5, 7, 9, 11]

Common element with least index sum in JavaScript

AmitDiwan
Updated on 21-Apr-2021 10:26:29

174 Views

ProblemWe are required to write a JavaScript function that takes in two arrays of literals, arr1 and arr2, as the first and the second argument.Our function should find out the common element in arr1 and arr2 with the least list index sum. If there is a choice tie between answers, we should output all of them with no order requirement.For example, if the input to the function is− Live Democonst arr1 = ['a', 'b', 'c', 'd']; const arr2 = ['d', 'a', 'c'];Then the output should be −const output = ['a'];Output ExplanationSince 'd' and 'a' are common in both the arrays the ... Read More

Changing second half of string number digits to zero using JavaScript

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

190 Views

ProblemWe are required to write a JavaScript function that takes in a string number as the only argument.Our function should return the input number with the second half of digits changed to 0.In cases where the number has an odd number of digits, the middle digit onwards should be changed to 0.For example −938473 → 938000ExampleFollowing is the code − Live Democonst num = '938473'; const convertHalf = (num = '') => {    let i = num.toString();    let j = Math.floor(i.length / 2);    if (j * 2 === i.length) {       return parseInt(i.slice(0, j) + '0'.repeat(j)); ... Read More

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 ]

Advertisements