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 88 of 534
Inverting signs of integers in an array using JavaScript
Problem We are required to write a JavaScript function that takes in an array of integers (negatives and positives). Our function should convert all positives to negatives and all negatives to positives and return the resulting array. Method 1: Using for Loop The traditional approach uses a for loop to iterate through each element and multiply by -1 to invert the sign: const arr = [5, 67, -4, 3, -45, -23, 67, 0]; const invertSigns = (arr = []) => { const res = []; for(let i = ...
Read MoredivisibleBy() function over array in JavaScript
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 MoreImplementing partial sum over an array using JavaScript
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 MoreSwitching positions of selected characters in a string in JavaScript
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 MoreRemoving the second number of the pair that add up to a target in JavaScript
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 MoreInterchanging first letters of words in a string in JavaScript
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 MorePerforming power operations on an array of numbers in JavaScript
We need to write a JavaScript function that takes an array of integers with even length and performs a specific power operation to find two numbers whose squares sum to a calculated value. Problem Statement Given an array of integers with even length, we calculate: num = (arr[0]² + arr[1]²) × (arr[2]² + arr[3]²) × ... × (arr[n-2]² + arr[n-1]²) Our function should return an array [A, B] such that A² + B² = num. Example Walkthrough For array [1, 2, 3, 4]: First pair: 1² + 2² = 1 ...
Read MoreFinding the product of array elements with reduce() in JavaScript
We are required to write a JavaScript function that takes in an array and finds the product of all its elements using the reduce() method. Understanding reduce() for Products The reduce() method executes a reducer function on each array element, accumulating results into a single value. For multiplication, we start with an initial value of 1 and multiply each element. Syntax array.reduce((accumulator, currentValue) => { return accumulator * currentValue; }, 1); Example: Basic Product Calculation const arr = [3, 1, 4, 1, 2, -2, -1]; const ...
Read MoreEncoding decimal to factorial and back in JavaScript
The factorial number system uses factorials as bases instead of powers. Each digit position represents a factorial base, where the rightmost digit is base 0!, the next is base 1!, then base 2!, and so on. How Factorial Encoding Works In factorial representation, the nth digit from the right can range from 0 to n. For example: Position 0 (rightmost): 0 to 0, multiplied by 0! = 1 Position 1: 0 to 1, multiplied by 1! = 1 Position 2: 0 to 2, multiplied by 2! = ...
Read MoreDifference between numbers and string numbers present in an array in JavaScript
In JavaScript, arrays can contain both numbers and string representations of numbers. This article demonstrates how to differentiate between these types and perform different operations based on their type. Problem We need to create a JavaScript function that takes a mixed array of numbers and string representations of integers. The function should add up all numeric values and subtract the sum of string numeric values from this total. Approach We'll use typeof to identify data types and the unary plus operator (+) to convert strings to numbers while checking if they're valid numeric strings. Example ...
Read More