Web Development Articles

Page 37 of 801

Squared and square rooted sum of numbers of an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 440 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function should take each number in the array and square it if it is even, or square root the number if it is odd and then return the sum of all the new numbers rounded to two decimal places.ExampleFollowing is the code −const arr = [45, 2, 13, 5, 14, 1, 20]; const squareAndRootSum = (arr = []) => {    const res = arr.map(el => {       if(el % 2 === 0){          return el * el;       }else{          return Math.sqrt(el);       };    });    const sum = res.reduce((acc, val) => acc + val);    return sum; }; console.log(squareAndRootSum(arr));Output613.5498231854631

Read More

Turn each character into its ASCII character code and join them together to create a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 405 Views

ProblemWe are required to write a JavaScript function that takes in a string. Our function should turn each character into its ASCII character code and join them together to create a number. Then we should replace all instances of 7 from this number to 1 to construct another number. Finally, we should return the difference of both these numbersExampleFollowing is the code −const str = 'AVEHDKDDS'; const ASCIIDifference = (str = '') => {    return str    .split('')    .map(c => c.charCodeAt(0))    .join('')    .split('')    .map(Number)    .filter(str => str === 7)    .length * 6; }; console.log(ASCIIDifference(str));Output12

Read More

Unique pairs in array that forms palindrome words in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 193 Views

ProblemWe are required to write a JavaScript function that takes in an array of unique words.Our function should return an array of all such index pairs, the words at which, when combined yield a palindrome word.ExampleFollowing is the code −const arr = ["abcd", "dcba", "lls", "s", "sssll"]; const findPairs = (arr = []) => {    const res = [];    for ( let i = 0; i < arr.length; i++ ){       for ( let j = 0; j < arr.length; j++ ){          if (i !== j ) {             let k = `${arr[i]}${arr[j]}`;             let l = [...k].reverse().join('');             if (k === l)             res.push( [i, j] );          }       };    };    return res; }; console.log(findPairs(arr));Output[ [ 0, 1 ], [ 1, 0 ], [ 2, 4 ], [ 3, 2 ] ]

Read More

Sorting according to number of 1s in binary representation using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 206 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function should sort the numbers according to decreasing number of 1s present in the binary representation of those numbers and return the new array.ExampleFollowing is the code −const arr = [5, 78, 11, 128, 124, 68, 6]; const countOnes = (str = '') => {    let count = 0;    for(let i = 0; i < str.length; i++){       const el = str[i];       if(el === '1'){          count++;       };    };    return count; }; const sortByHighBit = (arr = []) => {    arr.sort((a, b) => countOnes(b) - countOnes(a));    return arr; }; console.log(sortByHighBit(arr));Output[ 5, 78, 11, 128, 124, 68, 6 ]

Read More

Moving all vowels to the end of string using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 384 Views

ProblemWe are required to write a JavaScript function that takes in a string. Our function should construct a new string in which all the consonants should hold their relative position and all the vowels should be pushed to the end of string.ExampleFollowing is the code −const str = 'sample string'; const moveVowels = (str = '') => {    const vowels = 'aeiou';    let front = '';    let rear = '';    for(let i = 0; i < str.length; i++){       const el = str[i];       if(vowels.includes(el)){          rear += el;       }else{          front += el;       };    };    return front + rear; }; console.log(moveVowels(str));Outputsmpl strngaei

Read More

Finding number of open water taps after n chances using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 136 Views

ProblemSuppose a school organises this game on their Annual Day celebration −There are ”n” water taps and “n” students are chosen at random. The instructor asks the first student to go to every tap and open it. Then he has the second student go to every second tap and close it. The third goes to every third tap and, if it is closed, he opens it, and if it is open, he closes it. The fourth student does this to every fourth tap, and so on. After the process is completed with the "n"th student, how many taps are open?We ...

Read More

Sum of individual even and odd digits in a string number using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 634 Views

ProblemWe are required to write a JavaScript function that takes in string containing digits and our function should return true if the sum of even digits is greater than that of odd digits, false otherwise.ExampleFollowing is the code −const num = '645457345'; const isEvenGreater = (str = '') => {    let evenSum = 0;    let oddSum = 0;    for(let i = 0; i < str.length; i++){       const el = +str[i];       if(el % 2 === 0){          evenSum += el;       }else{          oddSum += el;       };    };    return evenSum > oddSum; }; console.log(isEvenGreater(num));Outputfalse

Read More

Finding the sum of all numbers in the nth row of an increasing triangle using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 279 Views

Increasing TriangleFor the purpose of this problem, suppose an increasing triangle to be like this −   1   2 3  4 5 6 7 8 9 10ProblemWe are required to write a JavaScript function that takes in a number n and returns the sum of numbers present in the nth row of increasing triangle.ExampleFollowing is the code −const num = 15; const rowSum = (num = 1) => {    const arr = [];    const fillarray = () => {       let num = 0;       for(let i = 1; i a + b, 0); }; console.log(rowSum(num));Output1695

Read More

Sum all perfect cube values upto n using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 314 Views

ProblemWe are required to write a JavaScript function that takes in a number n and returns the sum of all perfect cube numbers smaller than or equal to n.ExampleFollowing is the code −const num = 23546; const sumPerfectCubes = (num = 1) => {    let i = 1;    let sum = 0;    while(i * i * i

Read More

Reversing the bits of a decimal number and returning new decimal number in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 308 Views

ProblemWe are required to write a JavaScript function that takes in a decimal number, converts it into binary and reverses its 1 bit to 0 and 0 to 1 and returns the decimal equivalent of new binary thus formed.ExampleFollowing is the code −const num = 45657; const reverseBitsAndConvert = (num = 1) => {    const binary = num.toString(2);    let newBinary = '';    for(let i = 0; i < binary.length; i++){       const bit = binary[i];       newBinary += bit === '1' ? '0' : 1;    };    const decimal = parseInt(newBinary, 2);    return decimal; }; console.log(reverseBitsAndConvert(num));Output19878

Read More
Showing 361–370 of 8,006 articles
« Prev 1 35 36 37 38 39 801 Next »
Advertisements