Javascript Articles

Page 22 of 534

Binary array to corresponding decimal in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 413 Views

ProblemWe are required to write a JavaScript function that takes in a binary array (consisting of only 0 and 1).Our function should first join all the bits in the array and then return the decimal number corresponding to that binary.ExampleFollowing is the code −const arr = [1, 0, 1, 1]; const binaryArrayToNumber = arr => {    let num = 0;    for (let i = 0, exponent = 3; i < arr.length; i++) {       if (arr[i]) {          num += Math.pow(2, exponent);       };       exponent--;    };    return num; }; console.log(binaryArrayToNumber(arr));Output11

Read More

Converting km per hour to cm per second using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 736 Views

ProblemWe are required to write a JavaScript function that takes in a number that specifies speed in kmph and it should return the equivalent speed in cm/s.ExampleFollowing is the code −const kmph = 12; const convertSpeed = (kmph) => {    const secsInHour = 3600;    const centimetersInKilometers = 100000;    const speed = Math.floor((kmph * centimetersInKilometers) / secsInHour);    return `Equivalent in cmps is: ${speed}`; }; console.log(convertSpeed(kmph));OutputEquivalent in cmps is: 333

Read More

Inverting signs of integers in an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 272 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers (negatives and positives).Our function should convert all positives to negatives and all negatives to positives and return the resulting array.ExampleFollowing is the code −const arr = [5, 67, -4, 3, -45, -23, 67, 0]; const invertSigns = (arr = []) => {    const res = [];    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(+el && el !== 0){          const inverted = el * -1;          res.push(inverted);       }else{          res.push(el);       };    };    return res; }; console.log(invertSigns(arr));Output[ -5, -67, 4, -3, 45, 23, -67, 0 ]

Read More

divisibleBy() function over array in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 190 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers and a single number as two arguments.Our function should filter the array to contain only those numbers that are divisible by the number provided as second argument and return the filtered array.ExampleFollowing is the code −const arr = [56, 33, 2, 4, 9, 78, 12, 18]; const num = 3; const divisibleBy = (arr = [], num = 1) => {    const canDivide = (a, b) => a % b === 0;    const res = arr.filter(el => {       return canDivide(el, num);    });    return res; }; console.log(divisibleBy(arr, num));Output[ 33, 9, 78, 12, 18 ]

Read More

Implementing partial sum over an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 405 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function should construct and return a new array in which each corresponding element is the sum of all the elements right to it (including it) in the input array.ExampleFollowing is the code −const arr = [5, 6, 1, 3, 8, 11]; const partialSum = (arr = []) => {    let sum = arr.reduce((acc, val) => acc + val);    const res = [];    let x = 0;    if(arr.length === 0){       return [0];    }    for(let i = 0; i

Read More

Implementing custom function like String.prototype.split() function in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

ProblemWe are required to write a JavaScript function that lives on the prototype object of the String class.It should take in a string separator as the only argument (although the original split function takes two arguments). And our function should return an array of parts of the string separated and split by the separator.ExampleFollowing is the code −const str = 'this is some string'; String.prototype.customSplit = (sep = '') => {    const res = [];    let temp = '';    for(let i = 0; i < str.length; i++){       const el = str[i];       ...

Read More

Finding sum of sequence upto a specified accuracy using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 207 Views

ProblemSuppose the following sequence:Seq: 1/1 , 1/1x2 , 1/1x2x3 , 1/1x2x3x4 , ....The nth term of this sequence will be −1 / 1*2*3 * ... nWe are required to write a JavaScript function that takes in a number n, and return the sum of first n terms of this sequence.ExampleFollowing is the code −const num = 12; const seriesSum = (num = 1) => {    let m = 1;    let n = 1;    for(let i = 2; i < num + 1; i++){       m *= i;       n += (m * -1);    };    return n; }; console.log(seriesSum(num));Output-522956311

Read More

Switching positions of selected characters in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 424 Views

ProblemWe are required to write a JavaScript function that takes in a string that contains only the letter ‘k’, ‘l’ and ‘m’.The task of our function is to switch the positions of k with that of l leaving all the instances of m at their positions.ExampleFollowing is the code −const str = 'kklkmlkk'; const switchPositions = (str = '') => {    let res = "";    for(let i = 0; i < str.length; i++){       if (str[i] === 'k') {          res += 'l';       } else if (str[i] === 'l') {          res += 'k';       } else {          res += str[i];       };    };    return res; }; console.log(switchPositions(str));OutputFollowing is the console output −llklmkll

Read More

Finding smallest sum after making transformations in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 197 Views

ProblemWe are required to write a JavaScript function that takes in an array of positive integers. We can transform its elements by running the following operation on them as many times as required −if arr[i] > arr[j] then arr[i] = arr[i] - arr[j]When no more transformations are possible, our function should return its sum.ExampleFollowing is the code −const arr = [6, 9, 21]; const smallestSum = (arr = []) => {    const equalNums = arr => arr.reduce((a, b) => {       return (a === b) ? a : NaN;    });    if(equalNums(arr)){       return ...

Read More

Number of carries required while adding two numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 394 Views

ProblemWe are required to write a JavaScript function that takes in two numbers.Our function should count the number of carries we are required to take while adding those numbers as if we were adding them on paper.Like in the following image while adding 179 and 284, we had used carries two times, so for these two numbers, our function should return 2.ExampleFollowing is the code −const num1 = 179; const num2 = 284; const countCarries = (num1 = 1, num2 = 1) => {    let res = 0;    let carry = 0;    while(num1 + num2){     ...

Read More
Showing 211–220 of 5,339 articles
« Prev 1 20 21 22 23 24 534 Next »
Advertisements