Finding Nth Element of the Padovan Sequence Using JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:48:27

269 Views

Padovan SequenceThe Padovan sequence is the sequence of integers P(n) defined by the initial values −P(0) = P(1) = P(2) = 1and the recurrence relation,P(n) = P(n-2) + P(n-3)The first few values of P(n) are1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37, 49, 65, 86, 114, 151, 200, 265, …ProblemWe are required to write a JavaScript function that takes in a number n and return the nth term of the Padovan sequence.ExampleFollowing is the code − Live Democonst num = 32; const padovan = (num = 1) => {    let secondPrev = 1, pPrev = 1, pCurr = 1, pNext = 1;    for (let i = 3; i

Find All Possible Prime Pairs That Sum to Input Number Using JavaScript

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

567 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should return an array of all such number pairs that when summed are n and both of them are prime.ExampleFollowing is the code − Live Democonst num = 26; const isPrime = (n) => {    if (n % 2 === 0) return false;    let sqrtn = Math.sqrt(n)+1;    for (let i=3; i < sqrtn; i+=2) {       if (n % i === 0) return false;    }    return true; } const primeList = (a) => {    if (isPrime(a)) return ... Read More

Finding Immediate Next Character to a Letter in String Using JavaScript

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

1K+ Views

ProblemWe are required to write a JavaScript function that takes in a string of characters, str, and a single character, char.Our function should construct a new string that contains the immediate next character present in str after each instance of char (if any).ExampleFollowing is the code − Live Democonst str = 'this is a string'; const letter = 'i'; const findNextString = (str = '', letter = '') => {    let res = '';    for(let i = 0; i < str.length; i++){       const el = str[i];       const next = str[i + 1];       if(letter === el && next){          res += next;       };    };    return res; }; console.log(findNextString(str, letter));Outputssn

Remove Least Number of Elements to Convert Array into Increasing Sequence Using JavaScript

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

178 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function should try and remove the least number of elements from the array so that the array becomes an increasing sequence.ExampleFollowing is the code − Live Democonst arr = [1, 100, 2, 3, 100, 4, 5]; const findIncreasingArray = (arr = []) => {    const copy = arr.slice();    for(let i = 0; i < copy.length; i++){       const el = arr[i];       const next = arr[i + 1];       if(el > next){          copy[i] = undefined;       };    };    return copy.filter(Boolean); }; console.log(findIncreasingArray(arr));Output[ 1, 2, 3, 4, 5 ]

JavaScript Function on Array Prototype Object

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

119 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

599 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

298 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

299 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

265 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

622 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

Advertisements