Change Second Half of String Number Digits to Zero Using JavaScript

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

210 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

176 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

Sort Array and Find Sum of Differences Using JavaScript

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

369 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

Check if Digit is Divisible by Previous Digit in JavaScript

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

235 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

256 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

Finding 1-Based Index of a Character in Alphabets Using JavaScript

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

218 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

Implement Custom Filter Function in JavaScript

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

950 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

185 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

Calculate and Add Parity Bit to Binary Using JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:51:26

735 Views

Parity BitA parity bit, or check bit, is a bit added to a string of bits to ensure that the total number of 1-bits in the string is even or odd.ProblemWe are required to write a JavaScript function that takes in two parameters, one being the wanted parity (always 'even' or 'odd'), and the other being the binary representation of the number we want to check.The task of our function is to return an integer (0 or 1), which is the parity bit we need to add to the binary representation so that the parity of the resulting string is ... Read More

Sort Binary String with Even Decimal Value Using JavaScript

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

210 Views

ProblemWe are required to write a JavaScript function that takes in a string that contains binary strings of length 3 all separated by spaces.Our function should sort the numbers in ascending order but only order the even numbers and leave all odd numbers in their place.ExampleFollowing is the code − Live Democonst str = '101 111 100 001 010'; const sortEvenIncreasing = (str = '') => {    const sorter = (a, b) => {       const findInteger = bi => parseInt(bi, 2);       if(findInteger(a) % 2 === 1 || findInteger(b) % 2 === 1){          return 0;       };       return findInteger(a) - findInteger(b);    };    const res = str    .split(' ')    .sort(sorter)    .join(' ');    return res; }; console.log(sortEvenIncreasing(str));Output101 111 100 001 010

Advertisements