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
Web Development Articles
Page 200 of 801
Splitting number to contain continuous odd or even number using JavaScript
Problem We need to write a JavaScript function that takes a positive number and splits it into continuous segments of odd or even digits. The function should create a new segment whenever it encounters a digit with different parity (odd becomes even or even becomes odd). Example For the number 124579: 1 (odd) - starts first segment 2, 4 (even) - starts second segment 5, 7, 9 (odd) - starts third segment Solution Following is the code: const num = 124579; const splitDifferent = (num = 1) => { const ...
Read MoreSumming up digits and finding nearest prime in JavaScript
We are required to write a JavaScript function that takes in a number, finds the sum of its digits and returns a prime number that is just greater than or equal to the sum. This problem involves two main steps: calculating the digit sum and finding the nearest prime number greater than or equal to that sum. Breaking Down the Solution Our solution requires three functions: digitSum() - calculates the sum of all digits in a number isPrime() - checks if a number is prime nearestPrime() - finds the nearest prime >= digit sum ...
Read MoreConstructing a sentence based on array of words and punctuations using JavaScript
We need to write a JavaScript function that takes an array of words and punctuations and constructs a proper sentence following specific spacing and punctuation rules. Problem Statement Our function should join array elements to construct a sentence based on the following rules: There must always be a space between words There must not be a space between a comma and the word on the left There must always be one and only one period at the end of a sentence Solution Here's how ...
Read MoreTaking common elements from many arrays in JavaScript
We are required to write a JavaScript function that takes in any arbitrary number of arrays and returns an array of elements that are common to all arrays. If there are no common elements, then we should return an empty array. This problem requires finding the intersection of multiple arrays, which means identifying elements that exist in every provided array. Understanding the Problem The intersection of arrays means finding elements that appear in all arrays. For example, if we have arrays [1, 2, 3], [2, 3, 4], and [3, 4, 5], the common element is 3. ...
Read MoreFinding 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 MoreRepeated sum of Number's digits in JavaScript
We are required to write a JavaScript function that recursively sums up the digits of a number until it reduces to a single digit number. We are required to do so without converting the number to String or any other data type. How It Works The algorithm uses two functions: sumDigit(): Recursively extracts and sums all digits of a number sumRepeatedly(): Repeatedly calls sumDigit() until result is a single digit Example Let's implement the solution step by step: const num = 546767643; const sumDigit = (num, sum = 0) => ...
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 MoreReorder an array in JavaScript
Reordering an array means changing the sequence of elements within the array. JavaScript provides several built-in methods to reorder arrays, each serving different purposes. Using sort() for Alphabetical Ordering The sort() method sorts array elements alphabetically in ascending order by default. It modifies the original array. const states = ["Telangana", "Uttar Pradesh", "Karnataka", "Kerala", "TamilNadu"]; document.getElementById("para1").innerHTML = "Original: " + states; states.sort(); ...
Read MoreSorting Array without using sort() in JavaScript
Sorting arrays without using the built-in sort() method is a common programming challenge that helps understand sorting algorithms. JavaScript provides several approaches to implement custom sorting logic. Using Array.reduce() for Insertion Sort The reduce() method can implement insertion sort by building a sorted array incrementally: const arr = [4, 56, 5, 3, 34, 37, 89, 57, 98]; const sortWithReduce = arr => { return arr.reduce((acc, val) => { let ind = 0; while(ind < acc.length ...
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 More