Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Javascript Articles
Page 90 of 534
Finding the largest 5 digit number within the input number using JavaScript
We need to write a JavaScript function that takes in a string number of at least five digits and returns the greatest sequence of five consecutive digits found within the input number. Problem Statement Given a number string, find all possible 5-digit consecutive sequences and return the largest one as a number. Example Let's implement the solution step by step: const num = '123546544'; const findGreatestFiveDigit = (num = '') => { const str = num.toString(); const arr = []; ...
Read MoreReturning array values that are not odd in JavaScript
Problem We are required to write a JavaScript function that takes in an array of numbers. Our function should construct and return a new array that contains all the numbers of the input array that are not odd (i.e., even numbers). Example Following is the code − const arr = [5, 32, 67, 23, 55, 44, 23, 12]; const findNonOdd = (arr = []) => { const res = []; for(let i = 0; i < arr.length; i++){ ...
Read MoreConstructing a string of alternating 1s and 0s of desired length using JavaScript
Problem We 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. Example Following is the code − const num = 12; const buildString = (num = 1) => { let res = ''; for(let i = 0; i < num; i++){ if(i % 2 === 0){ ...
Read MoreFinding the immediate next character to a letter in string using JavaScript
We 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). Problem Given a string and a target letter, we need to find all characters that immediately follow each occurrence of the target letter and combine them into a new string. Solution We'll iterate through the string and check each character. When we find a match with our target letter, we'll add ...
Read MoreMean of an array rounded down to nearest integer in JavaScript
Problem We need to write a JavaScript function that takes an array of numbers and returns the average (mean) of the array rounded down to the nearest integer using Math.floor(). Example Following is the code − const arr = [45, 23, 67, 68, 12, 56, 99]; const roundedMean = (arr = []) => { const { sum, count } = arr.reduce((acc, val) => { let { sum, count } = acc; count++; ...
Read MoreConverting decimal to binary or hex based on a condition in JavaScript
Problem We need to write a JavaScript function that takes in a number n and converts it based on a condition: If a number is even, convert it to binary. If a number is odd, convert it to hex. Solution We can use JavaScript's toString() method with different radix values to perform the conversion. For binary conversion, we use radix 2, and for hexadecimal conversion, we use radix 16. Example Here's the implementation: const num = 1457; const conditionalConvert = (num = 1) ...
Read MoreCutting off number at each digit to construct an array in JavaScript
We need to write a JavaScript function that takes a number and returns an array of strings, where each string represents the number cut off at each digit position. Problem Given a number like 246, we want to create an array containing: First digit: "2" First two digits: "24" All digits: "246" Example Here's the implementation: const num = 246; const cutOffEach = (num = 1) => { const str = String(num); const res = []; let temp = ''; ...
Read MoreFinding two missing numbers that appears only once and twice respectively in JavaScript
We need to write a JavaScript function that finds two numbers in an array where all other numbers appear three times, except one number that appears twice and another that appears only once. Problem Statement Given an array where most numbers appear three times, find the two numbers that appear exactly twice and exactly once respectively. Example Let's solve this step by step: const arr = [1, 1, 1, 2, 2, 3]; const findMissing = (arr = []) => { let x = 0; // number appearing once ...
Read MoreFinding two prime numbers with a specific number gap in JavaScript
Finding prime numbers with a specific gap is a common programming challenge. This involves identifying two prime numbers where their difference equals a given value within a specified range. Problem We need to write a JavaScript function that takes a gap number and a range array as arguments. The function should return the first pair of prime numbers that have an absolute difference equal to the gap and fall within the specified range. Algorithm Approach The solution involves two main steps: Find all prime numbers within the given range Search for consecutive primes with the specified gap ...
Read MoreFinding value of a sequence for numbers in JavaScript
In JavaScript, we can calculate the value of mathematical sequences using loops and built-in Math functions. This article demonstrates how to implement a specific sequence formula that involves alternating signs, powers, and fractions. Problem Consider the following sequence sum: $$seq(n, \:p)=\displaystyle\sum\limits_{k=0}^{n}(-1)^{k}\times\:p\:\times 4^{n-k}\:\times\frac{2n-k}{k}$$ We need to write a JavaScript function that takes numbers n and p and returns the value of seq(n, p). The sequence includes alternating signs, exponential terms, and fractional components. Understanding the Formula The sequence contains several key components: (-1)^k - Creates alternating positive and negative terms p - A ...
Read More