Complete the code to calculate the remainder when dividing two numbers.
function mod(a, b) {
return a [1] b;
}The modulo operator % gives the remainder of division, which is often used in number theory problems.
Complete the code to check if a number is even.
function isEven(n) {
return n [1] 2 === 0;
}/ instead of modulo %.Using % 2 === 0 checks if the number divides evenly by 2, meaning it is even.
Fix the error in the function that calculates the greatest common divisor (GCD) using Euclid's algorithm.
function gcd(a, b) {
if (b === 0) return a;
return gcd(b, a [1] b);
}Euclid's algorithm uses the modulo operator % to find the remainder for the recursive step.
Fill both blanks to create a function that returns all prime numbers up to n.
function primesUpTo(n) {
const primes = [];
for (let i = 2; i [1] n; i++) {
let isPrime = true;
for (let j = 2; j [2] i; j++) {
if (i % j === 0) {
isPrime = false;
break;
}
}
if (isPrime) primes.push(i);
}
return primes;
}The outer loop iterates from 2 up to n inclusive using <=. The inner loop checks divisors from 2 up to but not including i using <.
Fill all three blanks to create a function that returns a dictionary of numbers and their squares for numbers greater than 3.
function squaresDict(numbers) {
return numbers.reduce((acc, num) => {
if (num [1] 3) {
acc[[2]] = [3];
}
return acc;
}, {});
}The function checks if num > 3, then sets the key as num and value as num * num in the dictionary.
