JavaScript Function on Array Prototype Object

AmitDiwan
Updated on 19-Apr-2021 11:47:00

107 Views

ProblemWe are required to write a JavaScript function that lives on the prototype object of the Array class. This function should take in a callback function and this function should return that very first element for which the callback function yields true.We should feed the current element and the current index to the callback function as first and second argument.ExampleFollowing is the code − Live Democonst arr = [4, 67, 24, 87, 15, 78, 3]; Array.prototype.customFind = function(callback){    for(let i = 0; i < this.length; i++){       const el = this[i];       if(callback(el, i)){     ... Read More

Construct String of Alternating 1s and 0s Using JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:46:42

584 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Starting with ‘1’ our function should construct a string of length n that contains ‘1’ and ‘0’ alternatingly.ExampleFollowing is the code − Live Democonst num = 12; const buildString = (num = 1) => {    let res = '';    for(let i = 0; i < num; i++){       if(i % 2 === 0){          res += 1;       }else{          res += 0;       };    };    return res; }; console.log(buildString(num));Output101010101010

Reverse Bits of a Decimal Number in JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:35:00

283 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 − Live Democonst 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

Sum All Perfect Cube Values Up to N Using JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:30:04

286 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 − Live Democonst num = 23546; const sumPerfectCubes = (num = 1) => {    let i = 1;    let sum = 0;    while(i * i * i

Sum of All Numbers in the Nth Row of an Increasing Triangle using JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:29:47

257 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 − Live Democonst 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

Sum of Individual Even and Odd Digits in a String Number Using JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:24:22

601 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 − Live Democonst 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

Finding Number of Open Water Taps After N Chances Using JavaScript

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

113 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

Sort Array with Weights Using JavaScript

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

384 Views

ProblemWe are required to write a JavaScript function that takes in an array of string that contains weights with three units: grams (G), kilo-grams (KG) and tonnes (T). Our function should sort the array into order of weight from light to heavy.We are required to write a JavaScript function that takes in an array of string that contains weights with three units: grams (G), kilo-grams (KG) and tonnes (T). Our function should sort the array into order of weight from light to heavy.ExampleFollowing is the code − Live Democonst arr = ['1456G', '1KG', '.5T', '.005T', '78645G', '23KG']; const arrangeWeights = (arr ... Read More

Move All Vowels to the End of String Using JavaScript

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

344 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 − Live Democonst 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

Sorting According to Number of 1s in Binary Representation Using JavaScript

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

190 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 − Live Democonst 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 ]

Advertisements