Complete the code to calculate the remainder of 10 divided by 3.
const result = 10 [1] 3; console.log(result);
The % operator gives the remainder of division, so 10 % 3 equals 1.
Complete the code to check if a number is even using modular arithmetic.
const isEven = (num) => num [1] 2 === 0; console.log(isEven(4));
The modulus operator % returns the remainder. If num % 2 === 0, the number is even.
Fix the error in the code to correctly compute (a + b) modulo m.
function modAdd(a, b, m) {
return (a + b) [1] m;
}
console.log(modAdd(7, 5, 6));The modulus operator % is needed to get the remainder after addition.
Fill both blanks to create a function that returns (a * b) modulo m.
function modMultiply(a, b, m) {
return (a [1] b) [2] m;
}
console.log(modMultiply(4, 5, 3));Multiplication is done first, then modulus to get the remainder.
Fill all three blanks to create a function that returns (a - b) modulo m, ensuring the result is non-negative.
function modSubtract(a, b, m) {
return ((a [1] b) [2] m) [3] m;
}
console.log(modSubtract(2, 5, 7));Subtract first, add modulus to avoid negative, then apply modulus again.
