Web Development Articles

Page 196 of 801

Listing all the prime numbers upto a specific number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 250 Views

We are required to write a JavaScript function that takes in a number, say n, and returns an array containing all the prime numbers up to n. For example: If the number n is 24. Then the output should be − const output = [2, 3, 5, 7, 11, 13, 17, 19, 23]; Therefore, let's write the code for this function − Method 1: Using Helper Function for Prime Check This approach uses a helper function to check if a number is prime, then iterates through numbers up to n: ...

Read More

divisibleBy() function over array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 200 Views

Problem We are required to write a JavaScript function that takes in an array of numbers and a single number as two arguments. Our function should filter the array to contain only those numbers that are divisible by the number provided as second argument and return the filtered array. Example Following is the code − const arr = [56, 33, 2, 4, 9, 78, 12, 18]; const num = 3; const divisibleBy = (arr = [], num = 1) => { const canDivide = (a, b) => a % ...

Read More

Finding product of Number digits in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 2K+ Views

We are required to write a JavaScript program that takes in a number and finds the product of all of its digits. Input Output Scenarios Here are examples of finding the product of number digits: Input = 12345 Output = 120 (1 × 2 × 3 × 4 × 5 = 120) We can also work with string numbers and convert them to integers: Input = "12345" Output = 120 Using Math.floor() and Modulo Operator The Math.floor() function returns the largest integer less than or equal to a given ...

Read More

Implementing partial sum over an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 420 Views

We need to write a JavaScript function that takes an array of numbers and returns a new array where each element represents the sum of all elements from that position to the end of the original array (including the current element). Problem Given an array of numbers, construct a new array where each corresponding element is the sum of all elements from that position to the right (including itself) in the input array. Example Input and Expected Output For array [5, 6, 1, 3, 8, 11], the partial sum array should be: Position 0: 5 ...

Read More

If the element repeats, remove all its instances from array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 152 Views

When working with arrays in JavaScript, sometimes you need to remove all instances of elements that appear more than once, keeping only elements that appear exactly once. This is different from typical deduplication where you keep one instance of each duplicate. For example, if the input is: const arr = [763, 55, 43, 22, 32, 43, 763, 43]; console.log("Original array:", arr); Original array: [ 763, 55, 43, 22, 32, 43, 763, 43 ] The output should be: [ 55, 22, 32 ] Notice that 763 and 43 are ...

Read More

Switching positions of selected characters in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 447 Views

We are required to write a JavaScript function that takes in a string containing only the letters 'k', 'l' and 'm'. The task is to switch the positions of 'k' with 'l' while leaving all instances of 'm' at their original positions. Problem Statement Given a string with characters 'k', 'l', and 'm', we need to swap every occurrence of 'k' with 'l' and vice versa, keeping 'm' unchanged. Example Here's the implementation using a simple loop approach: const str = 'kklkmlkk'; const switchPositions = (str = '') => { ...

Read More

Array flattening using loops and recursion in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 685 Views

Array flattening converts nested arrays into a single-level array. JavaScript provides multiple approaches including loops, recursion, and built-in methods. Problem Overview Given a nested array with mixed data types including falsy values, we need to flatten it completely: const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], [6]]; console.log("Input:", arr); Input: [ [ 1, 2, 3 ], [ 4, 5, [ 5, false, 6, [Array] ] ], [ 6 ] ] Expected output should be a flat array preserving all values including false and null: ...

Read More

Removing the second number of the pair that add up to a target in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 174 Views

Problem We need to write a JavaScript function that takes an array of numbers and a target sum. The function should remove the second number from any consecutive pair of numbers that add up to the target. Example Let's see how this works with a practical example: const arr = [1, 2, 3, 4, 5]; const target = 3; const removeSecond = (arr = [], target = 1) => { const res = [arr[0]]; for(let i = 1; i < arr.length; i++){ ...

Read More

Using recursion to remove consecutive duplicate entries from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 555 Views

We are supposed to write a function that takes in an array of number/string literals. The function should remove all the redundant consecutive elements of the array without using extra memory space. For example, if the input array is − const arr = [17, 17, 17, 12, 12, 354, 354, 1, 1, 1]; Then the output should be − const output = [17, 12, 354, 1]; Therefore, let's write the code for this function − How the Algorithm Works The recursive function works by checking each element against its ...

Read More

Interchanging first letters of words in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 376 Views

We need to write a JavaScript function that takes a string containing exactly two words and swaps their first letters to create a new string. Problem Statement Given a string with two words separated by a space, we want to interchange the first character of each word while keeping the rest of the characters in their original positions. Example For input "hello world", we should get "wello horld" where 'h' and 'w' are swapped. const str = 'hello world'; const interchangeChars = (str = '') => { const [first, second] ...

Read More
Showing 1951–1960 of 8,010 articles
« Prev 1 194 195 196 197 198 801 Next »
Advertisements