
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 6710 Articles for Javascript

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

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

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

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

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

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

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

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 ]

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