Found 6710 Articles for Javascript

Converting decimal to binary or hex based on a condition in JavaScript

AmitDiwan
Updated on 20-Apr-2021 06:34:14

200 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should convert the number to binary or hex based on −If a number is even, convert it to binary.If a number is odd, convert it to hex.ExampleFollowing is the code − Live Democonst num = 1457; const conditionalConvert = (num = 1) => {    const isEven = num % 2 === 0;    const toBinary = () => num.toString(2);    const toHexadecimal = () => num.toString(16);    return isEven       ? toBinary()       : toHexadecimal(); }; console.log(conditionalConvert(num));OutputFollowing is the console output −5b1

Finding the k-prime numbers with a specific distance in a range in JavaScript

AmitDiwan
Updated on 20-Apr-2021 06:30:49

410 Views

K-Prime NumbersA natural number is called k-prime if it has exactly k prime factors, counted with multiplicity.Which means even though the only prime factor of 4 is 2 it will be a 2-prime number because −4 = 2 * 2 and both 2s will be counted separately taking the count to 2.Similarly, 8 is 3-prime because 8 = 2 * 2 * 2 taking the count to 3.ProblemWe are required to write a JavaScript function that takes in a number k, a distance and a range.Our function should return an array of arrays containing k-prime numbers within the range the ... Read More

Sorting numbers in ascending order and strings in alphabetical order in an array in JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 09:42:03

1K+ Views

In the realm of JavaScript programming, the ability to sort numbers in ascending order and strings in alphabetical order within an array holds paramount importance. Efficiently arranging elements in such a manner not only enhances the organization and readability of data but also plays a pivotal role in various data manipulation and analysis tasks. By harnessing the power of JavaScript's built-in sorting capabilities, developers can effortlessly arrange array elements in the desired order. In this article, we will explore the intricacies of sorting numbers in ascending order and strings in alphabetical order within an array, unveiling the underlying mechanisms and ... Read More

Mapping array of numbers to an object with corresponding char codes in JavaScript

AmitDiwan
Updated on 20-Apr-2021 06:24:47

628 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. For each number in the array, we need to create an object. The object key will be the number, as a string. And the value will be the corresponding character code, as a string.We should finally return an array of the resulting objects.ExampleFollowing is the code − Live Democonst arr = [67, 84, 98, 112, 56, 71, 82]; const mapToCharCodes = (arr = []) => {    const res = [];    for(let i = 0; i < arr.length; i++){       const el = ... Read More

Mean of an array rounded down to nearest integer in JavaScript

AmitDiwan
Updated on 20-Apr-2021 06:22:24

364 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function is supposed to return the average of the given array rounded down to its nearest integer.ExampleFollowing is the code − Live Democonst arr = [45, 23, 67, 68, 12, 56, 99]; const roundedMean = (arr = []) => {    const { sum, count } = arr.reduce((acc, val) => {       let { sum, count } = acc;       count++;       sum += val;       return { sum, count };    }, {       sum: 0, count: 0    });    const mean = sum / (count || 1);    return Math.round(mean); }; console.log(roundedMean(arr));OutputFollowing is the console output −53

Finding nth element of the Padovan sequence using JavaScript

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

241 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

Finding all possible prime pairs that sum upto input number using JavaScript

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

553 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 the 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

Removing least number of elements to convert array into increasing sequence using JavaScript

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

162 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 that lives on the prototype object of the Array class

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

100 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

Advertisements