Complete the code to calculate the remainder when dividing a number by 5.
const remainder = num [1] 5;
The % operator gives the remainder of division, which is key in many number theory problems.
Complete the code to check if a number is even using modulus.
if (num [1] 2 === 0) { console.log('Even number'); }
The modulus operator % with 2 checks if a number is even by seeing if the remainder is zero.
Fix the error in the code to compute the greatest common divisor (GCD) using Euclid's algorithm.
function gcd(a: number, b: number): number {
if (b === 0) return a;
return gcd(b, a [1] b);
}Euclid's algorithm uses the remainder operator % to find the GCD.
Fill both blanks to create a function that returns a list of prime numbers up to n.
function primesUpTo(n: number): number[] {
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 runs from 2 up to and including n (<=), and the inner loop checks divisors less than i (<).
Fill all three blanks to create a function that returns a dictionary of numbers and their squares for numbers greater than 3.
function squaresMap(nums: number[]): {[key: number]: number} {
return nums.reduce((acc, num) => {
if (num [1] 3) {
acc[num] = num [2] num;
}
return acc;
}, {} as {[key: number]: number});
}The condition checks if the number is greater than 3 (>), and the square is calculated by multiplying the number by itself (*).
