Open In App

JavaScript Program to Find Remainder of Array Multiplication Divided by n

Last Updated : 22 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, the remainder of the array multiplication when divided by the number n can be found by iteration, functional programming or array reduction. , it’s often necessary to calculate the remainder of the product of all elements in an array when divided by a given number ‘n’. There are various possible approaches in JavaScript to achieve this which are as follows:

Using forEach() method

This approach iterates through each element of the array using the forEach() method and calculates the remainder of the product of all elements when divided by a given number ‘n’.

The forEach() method iterates over elements of an array and executes a provided function once for each array element. It allows you to perform a specific action on each element of the array without mutating the array itself.

Example: To demonstrate finding the remainder of the array multiplication using forEach() method.

Javascript




function findRemainder1(arr, n) {
    let product = 1;
    arr.forEach((num) => (product *= num));
    return product % n;
}
 
let arr1 = [3, 7, 2, 5];
let n1 = 11;
console.log(findRemainder1(arr1, n1));


Output

1

Time Complexity: O(n)

Space Complexity: O(1)

Using reduce() function

This method uses the reduce() function to accumulate the product of all array elements and then calculates the remainder when divided by a specified number ‘n’.

The reduce() method executes a reducer function on each element of the array, resulting in a single output value. It accumulates the result of the function calls on each element, providing the final result.

Example: This example illustrates finding the remainder of the array multiplication divided by ‘n’ in JavaScript using the reduce() method.

Javascript




function findRemainder2(arr, n) {
    let product = arr.reduce((acc, val) => {
        acc * val, 1
    });
    return product % n;
}
 
let arr2 = [4, 8, 6, 2];
let n2 = 13;
console.log(findRemainder2(arr2, n2));


Output

NaN

Time Complexity: O(n)

Space Complexity: O(1)

Using functional approach with reduce()

In this approach, a functional programming style is employed along with the reduce() function to find the remainder of the array multiplication when divided by a given number ‘n’.

Example: Here, the code showcases how to find the remainder of the array multiplication divided by ‘n’ in JavaScript using the reduce() method with a functional approach.

Javascript




function findRemainder3(arr, n) {
    return arr.reduce((acc, val) => {
        (acc * val) % n, 1
    });
}
 
 
let arr3 = [1, 2, 3, 4];
let n3 = 17;
console.log(findRemainder3(arr3, n3));


Output

undefined

Time Complexity: O(n)

Space Complexity: O(1)

Using traditional for-loop

This approach utilizes a traditional for-loop to traverse the array and compute the product of all elements, followed by finding the remainder when divided by a provided number ‘n’.

Example: To demonstrates how to find the remainder of the array multiplication divided by ‘n’ in JavaScript using a for loop.

Javascript




function findRemainder4(arr, n) {
    let product = 1;
    for (let num of arr) {
        product *= num;
        product %= n;
    }
    return product;
}
 
let arr4 = [2, 4, 6, 8];
let n4 = 19;
console.log(findRemainder4(arr4, n4));


Output

4

Time Complexity: O(n)

Space Complexity: O(1)



Similar Reads

Find quotient and remainder by dividing an integer in JavaScript
In this article, we will find the quotient and remainder by dividing an integer in JavaScript. There are various methods to divide an integer number by another number and get its quotient and remainder. Using Math.floor() methodUsing ~~ operatorright shift >> operator Example 1: This example uses the Math.floor() function to calculate the div
1 min read
JavaScript Program for Multiplication of Two Numbers
In this article, we are going to learn how we can multiply two numbers using JavaScript. Multiplying the two numbers in JavaScript involves performing arithmetic addition on numeric values, resulting in their sum. JavaScript supports numeric data types. In JavaScript, both floating-point and integer numbers fall under the same "Number" type. Becaus
4 min read
JavaScript Program to print multiplication table of a number
In this article, we are given a number n as input, we need to print its table. Examples: Input : 5 Output : 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50 Input : 8 Output : 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 8 * 9 = 72 8 * 10 = 80 8 *
4 min read
JavaScript Remainder(%) Operator
The remainder operator in JavaScript is used to get the remaining value when an operand is divided by another operand. In some languages, % is considered modulo. Modulo and Remainder work differently when the sign of both operands is different. In JavaScript remainder takes the sign of the dividend and to get modulo ((a % n) + n) % n should be used
2 min read
JavaScript Remainder Assignment(%=) Operator
JavaScript remainder assignment operator (%=) assigns the remainder to the variable after dividing a variable by the value of the right operand. Syntax: Operator: x %= y Meaning: x = x % y Below example illustrate the Remainder assignment(%=) Operator in JavaScript: Example 1: The following example demonstrates if the given number is divisible by 4
1 min read
Remainder Assignment(%=) Operator in Javascript
The Remainder Assignment Operator in javascript is represented by "%=". This operator is used to divide the value of the variable by another operand and assign the remainder to the variable which is the first operand. This can be also explained as assigning the remainder to the first operand which is obtained by dividing the variable from the secon
1 min read
Multiplication Assignment(*=) Operator in JavaScript
Multiplication Assignment Operator(*=) in JavaScript is used to multiply two operands and assign the result to the right operand. Syntax: variable1 *= variable2 // variable1 = variable1 * variable2 Example 1: In this example, we multiply two numerical values using the Multiplication Assignment Operator(*=) and assign the result variable in javascri
1 min read
Multiplication(*) Arithmetic Operator in JavaScript
JavaScript arithmetic multiplication operator is used to find the product of operands. Like if we want the multiplication of any two numbers then this operator can be useful. Syntax: a*b Return: It returns the roduct of the operands. Example 1: In this example, we will perform multiplication on Number. C/C++ Code console.log(100*20); console.log(In
1 min read
Multiplication Quiz Webapp using HTML CSS and JavaScript
In this article, we will see how to create a multiplication quiz web app using HTML, CSS, and JavaScript. Description of Web App: In this web app, random questions come one by one based on basic multiplication. If you give a correct answer, then it will increment by +1, and if you give the wrong answer, then it will decrement by -1. File Structure:
3 min read
Matrix Chain Multiplication by using JavaScript
In this article, we are going to learn about Matrix Chain Multiplication by using JavaScript, Matrix Chain Multiplication refers to finding the most efficient way to multiply a sequence of matrices together, minimizing the total number of scalar multiplications required. Approaches to perform Matrix Chain Multiplication by using JavaScriptRecursive
2 min read
Article Tags :