How to check if one number is multiple of another in JavaScript

Check if one Number is Multiple of another in JavaScript

In this article, you’ll learn how to check if a number is a multiple of another in JavaScript by using the remainder operator. Let’s go into detail now.

Check if one number is multiple of another in JavaScript

Using a remainder operator (%)

Syntax: x % y

The remainder operator (or sometimes called the modulo operator) divides two numbers and returns its remainder. By that logic, if a number when divided by another number returns a 0 as its remainder, the former number is therefore a multiple of the latter number.

As such, the code would look something like this:

Code:

const n1 = 10;
const n2 = 2;
const n3 = 3;

function isMultiple(divided, divider) {
	return divided % divider === 0; // If the remainder is 0, it's a multiple
}

// Check n1 and n2
if (isMultiple(n1, n2)) {
	console.log(`${n1} is a multiple of ${n2}`);
} else {
	console.log(`${n1} is NOT a multiple of ${n2}`);
}

// Check n1 and n3
if (isMultiple(n1, n3)) {
	console.log(`${n1} is a multiple of ${n3}`);
} else {
	console.log(`${n1} is NOT a multiple of ${n3}`);
}

Output:

"10 is a multiple of 2"
"10 is NOT a multiple of 3"

Checking if a number is a multiple of several other numbers

That’s great, but what if you wanted to check if a number is a multiple of several numbers? For example: How do you check if every number in an array has a number as its multiple or which has that number as a multiple? You could do it manually, but that can be monstrously tedious. An automated method is required and can be found in using a function in combination with rest parameters.

The rest parameter syntax (...) allows a function to have a practically infinite number of parameters, which can then be iterated like an array. Iterating over the parameters can be done with a for...of loop, which specifically iterates over collections like arrays.

Using that method, we can check if a number is a multiple of several numbers; as such:

Code:

function isMultipleOf(product, ...numbers) {
	for (const number of numbers) {
		// If the remainder is 0, return true
		if (product % number === 0) {
			return true;
		}
	}
	return false;
}

console.log(isMultipleOf(12, 1, 4, 3, 2, 6));
console.log(isMultipleOf(12, 5));

Output:

true
false

Additionally, you use an array as a parameter, as long as you use a spread syntax (...) with it. The spread syntax takes a collection of items (e.g., arrays, objects) and converts it into a list of arguments (i.e., parameters). A spread syntax has other utilities, which you can read about here. For example:

Code:

function isMultipleOf(product, ...numbers) {
	for (const number of numbers) {
		// If the remainder is 0, return true
		if (product % number === 0) {
			return true;
		}
	}
	return false;
}

console.log(isMultipleOf(12, ...[1, 4, 3, 2, 6]));
console.log(isMultipleOf(12, ...[5]));

Output:

true
false

Additionally, if you want the function to instead return every number that has the number as its multiple, you can do like below:

Code:

function isMultipleOf(product, ...numbers) {
	const arr = [];

	for (const number of numbers) {
		// If the remainder is 0, add the number to the list
		if (product % number === 0) {
			arr.push(number);
		}
	}

	return arr;
}

console.log(isMultipleOf(12, ...[1, 4, 3, 2, 6]));
console.log(isMultipleOf(12, ...[5, 3, 6, 7, 1]));

Output:

[1, 4, 3, 2, 6]
[3, 6, 1]

Summary

To check if one number is multiple of another in JavaScript, you can use the remainder operator (%) and check whether the number returned is 0 or not. If the number returned is 0, then the number is a multiple of the other.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *