Leap Year Checker
A leap year is a year that is divisible by 4, except for years that are divisible by 100 but not by 400.
$$\text{{Leap year}} = (\text{{year}} \mod 4 = 0) \land (\text{{year}} \mod 100 \neq 0) \lor (\text{{year}} \mod 400 = 0)$$
function checkLeapYear() {
const year = parseInt(document.getElementById('leapYearInput').value);
const isLeap = (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
document.getElementById('leapYearResult').textContent = isLeap ? `${year} is a leap year.` : `${year} is not a leap year.`;
}
Input Handling
-
The function
checkLeapYear()
begins by retrieving the year entered by the user in the input field with the IDleapYearInput
. TheparseInt
function is used to convert this input from a string to an integer.
Leap Year Calculation
-
The code then calculates whether the entered year is a leap year by using the formula:
-
A year is a leap year if it is divisible by 4 and not divisible by 100, unless it is also divisible by 400. This is implemented in the condition:
-
const isLeap = (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
Result Display
-
The result of the leap year check is then displayed to the user by updating the text content of the element with the ID
leapYearResult
. If the year is a leap year, it displays "{year} is a leap year.
" Otherwise, it displays "{year} is not a leap year.
"
Prime Number Checker
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
$$\text{{Prime number}} = n > 1 \land \forall d \in [2, \sqrt{n}], (n \mod d \neq 0)$$
function checkPrime() {
const num = parseInt(document.getElementById('primeInput').value);
if (num <= 1) {
document.getElementById('primeResult').textContent = `${num} is not a prime number.`;
return;
}
let isPrime = true;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
isPrime = false;
break;
}
}
document.getElementById('primeResult').textContent = isPrime ? `${num} is a prime number.` : `${num} is not a prime number.`;
}
Input Handling
-
The function
checkPrime()
starts by retrieving the number entered by the user in the input field with the IDprimeInput
. TheparseInt
function is used to convert the input from a string to an integer. -
If the number is less than or equal to 1, it is immediately determined that the number is not prime, and the function returns with the message indicating this.
Prime Number Calculation
-
For a number greater than 1, the function checks if the number is divisible by any integer from 2 up to the square root of the number.
-
The loop iterates over all potential divisors
i
, and if the numbernum
is divisible by any of these, it is not a prime number. -
let isPrime = true;
This variable is initialized to track whether the number is prime. If a divisor is found,isPrime
is set tofalse
. -
The condition
if (num % i === 0)
checks if the number is divisible byi
. If true,isPrime
is set tofalse
, and the loop is exited usingbreak
.
Result Display
-
The result of the prime number check is displayed to the user by updating the text content of the element with the ID
primeResult
. If the number is prime, it displays "{num} is a prime number.
" Otherwise, it displays "{num} is not a prime number.
"
Odd or Even Checker
A number is considered even if it is divisible by 2 without a remainder. If a number is not divisible by 2, it is considered odd.
$$\text{{Even number}} = (n \mod 2 = 0)$$
$$\text{{Odd number}} = (n \mod 2 \neq 0)$$
function checkOddEven() {
const num = parseInt(document.getElementById('oddEvenInput').value);
document.getElementById('oddEvenResult').textContent = num % 2 === 0 ? `${num} is an even number.` : `${num} is an odd number.`;
}
Input Handling
-
The function
checkOddEven()
retrieves the number entered by the user in the input field with the IDoddEvenInput
. TheparseInt
function is used to convert the input from a string to an integer.
Odd or Even Calculation
-
The code checks whether the number is odd or even by evaluating the expression
num % 2 === 0
. -
If the remainder when the number is divided by 2 is 0, the number is even. Otherwise, it is odd.
-
This condition is directly used to determine the output:
document.getElementById('oddEvenResult').textContent = num % 2 === 0 ? `${num} is an even number.` : `${num} is an odd number.`;
Result Display
-
The result of the odd or even check is displayed by updating the text content of the element with the ID
oddEvenResult
. -
If the number is even, it displays "
{num} is an even number.
" Otherwise, it displays "{num} is an odd number.
"
Factorial Calculator
The factorial of a non-negative integer \( n \) is the product of all positive integers less than or equal to \( n \). The factorial is commonly used in permutations, combinations, and other areas of mathematics.
$$n! = n \times (n-1) \times (n-2) \times \ldots \times 1$$
function calculateFactorial() {
const num = parseInt(document.getElementById('factorialInput').value);
let result = 1;
for (let i = 2; i <= num; i++) {
result *= i;
}
document.getElementById('factorialResult').textContent = `${num}! = ${result}`;
}
Input Handling
-
The function
calculateFactorial()
retrieves the number entered by the user in the input field with the IDfactorialInput
. TheparseInt
function is used to convert the input from a string to an integer.
Factorial Calculation
-
The code initializes a variable
result
to 1, which will hold the final value of the factorial. -
A
for
loop iterates from 2 up to the number \( n \), multiplying the current value ofresult
by the loop variablei
at each step. -
This multiplication accumulates the product of all integers from 2 to \( n \), resulting in the factorial of \( n \).
Result Display
-
The result of the factorial calculation is displayed by updating the text content of the element with the ID
factorialResult
. -
The output format is "
{num}! = {result}
", where{num}
is the input number and{result}
is the calculated factorial.
GCD (Greatest Common Divisor) Calculator
The Greatest Common Divisor (GCD) of two numbers is the largest positive integer that divides both numbers without leaving a remainder. The GCD is useful in simplifying fractions and is a key concept in number theory.
$$\text{GCD}(a, b) = \max\{d : d | a \land d | b\}$$
function calculateGCD() {
const num1 = parseInt(document.getElementById('gcdInput1').value);
const num2 = parseInt(document.getElementById('gcdInput2').value);
const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));
const result = gcd(num1, num2);
document.getElementById('gcdResult').textContent = `GCD(${num1}, ${num2}) = ${result}`;
}
Input Handling
-
The function
calculateGCD()
retrieves two numbers entered by the user in the input fields with the IDsgcdInput1
andgcdInput2
. TheparseInt
function is used to convert these inputs from strings to integers.
GCD Calculation Using the Euclidean Algorithm
-
The GCD is calculated using the Euclidean algorithm, which is based on the principle that the GCD of two numbers does not change if the larger number is replaced by its difference with the smaller number.
-
The algorithm is implemented recursively in the following way:
const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));
-
This recursive function continues to call itself, reducing the problem size each time, until the second number
b
becomes zero. At this point, the first numbera
is the GCD.
Result Display
-
The result of the GCD calculation is displayed by updating the text content of the element with the ID
gcdResult
. -
The output format is "
GCD({num1}, {num2}) = {result}
", where{num1}
and{num2}
are the input numbers and{result}
is the calculated GCD.
LCM (Least Common Multiple) Calculator
The Least Common Multiple (LCM) of two numbers is the smallest positive integer that is divisible by both numbers. The LCM is useful in adding or subtracting fractions with different denominators and is an important concept in number theory.
$$\text{LCM}(a, b) = \frac{|a \times b|}{\text{GCD}(a, b)}$$
function calculateLCM() {
const num1 = parseInt(document.getElementById('lcmInput1').value);
const num2 = parseInt(document.getElementById('lcmInput2').value);
const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));
const lcm = (a, b) => Math.abs(a * b) / gcd(a, b);
const result = lcm(num1, num2);
document.getElementById('lcmResult').textContent = `LCM(${num1}, ${num2}) = ${result}`;
}
Input Handling
-
The function
calculateLCM()
retrieves two numbers entered by the user in the input fields with the IDslcmInput1
andlcmInput2
. TheparseInt
function is used to convert these inputs from strings to integers.
LCM Calculation Using the GCD
-
The LCM of two numbers is calculated using the relationship between LCM and GCD (Greatest Common Divisor):
$$\text{LCM}(a, b) = \frac{|a \times b|}{\text{GCD}(a, b)}$$
-
First, the GCD of the two numbers is calculated using the Euclidean algorithm, implemented as:
const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));
-
Then, the LCM is calculated by dividing the absolute product of the two numbers by their GCD:
const lcm = (a, b) => Math.abs(a * b) / gcd(a, b);
Result Display
-
The result of the LCM calculation is displayed by updating the text content of the element with the ID
lcmResult
. -
The output format is "
LCM({num1}, {num2}) = {result}
", where{num1}
and{num2}
are the input numbers and{result}
is the calculated LCM.
Square Root Calculator
The square root of a number \( n \) is a value \( x \) such that \( x^2 = n \). In other words, it is a number \( x \) that, when multiplied by itself, gives \( n \). The square root is a fundamental operation in mathematics, especially in algebra and geometry.
$$\sqrt{n} = x \quad \text{where} \quad x^2 = n$$
function calculateSquareRoot() {
const num = parseInt(document.getElementById('sqrtInput').value);
const result = Math.sqrt(num);
document.getElementById('sqrtResult').textContent = `√${num} = ${result}`;
}
Input Handling
-
The function
calculateSquareRoot()
retrieves the number entered by the user in the input field with the IDsqrtInput
. TheparseInt
function is used to convert the input from a string to an integer.
Square Root Calculation
-
The square root is calculated using the built-in
Math.sqrt()
function in JavaScript, which computes the square root of a given number. -
The result is the value \( x \) such that \( x^2 = n \).
-
This is a straightforward operation, but it is essential for various mathematical and computational problems.
Result Display
-
The result of the square root calculation is displayed by updating the text content of the element with the ID
sqrtResult
. -
The output format is "
√{num} = {result}
", where{num}
is the input number and{result}
is the calculated square root.
Exponentiation Calculator
Exponentiation is the mathematical operation involving two numbers, the base \( a \) and the exponent \( b \). It represents repeated multiplication of the base. For example, \( a^b \) means multiplying \( a \) by itself \( b \) times.
$$a^b = a \times a \times \ldots \times a \quad \text{(b times)}$$
function calculateExponent() {
const base = parseInt(document.getElementById('baseInput').value);
const exponent = parseInt(document.getElementById('exponentInput').value);
const result = Math.pow(base, exponent);
document.getElementById('exponentResult').textContent = `${base}^${exponent} = ${result}`;
}
Input Handling
-
The function
calculateExponent()
retrieves the base and exponent entered by the user in the input fields with the IDsbaseInput
andexponentInput
. TheparseInt
function is used to convert these inputs from strings to integers.
Exponentiation Calculation
-
The exponentiation is calculated using the built-in
Math.pow()
function in JavaScript, which computes the value of the base raised to the power of the exponent. -
For example, if the base is \( a \) and the exponent is \( b \), the result is \( a^b \).
-
Exponentiation is widely used in various fields such as algebra, computer science, and physics.
Result Display
-
The result of the exponentiation calculation is displayed by updating the text content of the element with the ID
exponentResult
. -
The output format is "
{base}^{exponent} = {result}
", where{base}
is the base number,{exponent}
is the exponent, and{result}
is the calculated power.
Sum of Digits Calculator
The sum of the digits of a number is the result of adding together all the individual digits that make up the number. This operation is useful in various algorithms and is often used in digital root calculations and checksums.
$$\text{Sum of digits} = \sum_{i=1}^{n} d_i \quad \text{where } d_i \text{ is the i-th digit of the number}$$
function calculateSumOfDigits() {
const num = parseInt(document.getElementById('sumDigitsInput').value);
const sum = num.toString().split('').reduce((acc, digit) => acc + parseInt(digit), 0);
document.getElementById('sumDigitsResult').textContent = `Sum of digits of ${num} = ${sum}`;
}
Input Handling
-
The function
calculateSumOfDigits()
retrieves the number entered by the user in the input field with the IDsumDigitsInput
. TheparseInt
function is used to convert this input from a string to an integer.
Sum of Digits Calculation
-
The number is first converted to a string using
toString()
, and then split into individual digits using thesplit('')
method. -
The
reduce()
function is then used to iterate over these digits, converting each digit back to an integer and summing them together. -
This operation results in the sum of all digits of the number.
Result Display
-
The result of the sum of digits calculation is displayed by updating the text content of the element with the ID
sumDigitsResult
. -
The output format is "
Sum of digits of {num} = {sum}
", where{num}
is the input number and{sum}
is the calculated sum of its digits.
Reverse a Number
Reversing a number involves rearranging its digits in reverse order. For example, if the number is 1234, the reversed number will be 4321. This operation is useful in various computational problems and algorithms.
If the number is \( n \), and its digits are \( d_1, d_2, \ldots, d_k \), then the reversed number is \( d_k, d_{k-1}, \ldots, d_1 \).
function reverseNumber() {
const num = parseInt(document.getElementById('reverseInput').value);
const reversed = parseInt(num.toString().split('').reverse().join(''));
document.getElementById('reverseResult').textContent = `Reversed number of ${num} = ${reversed}`;
}
Input Handling
-
The function
reverseNumber()
retrieves the number entered by the user in the input field with the IDreverseInput
. TheparseInt
function is used to convert this input from a string to an integer.
Number Reversal Process
-
The number is first converted to a string using
toString()
, and then split into individual digits using thesplit('')
method. -
The digits are reversed using the
reverse()
method, and then joined back together into a string usingjoin('')
. -
Finally, the reversed string is converted back into a number using
parseInt()
.
Result Display
-
The result of the number reversal is displayed by updating the text content of the element with the ID
reverseResult
. -
The output format is "
Reversed number of {num} = {reversed}
", where{num}
is the input number and{reversed}
is the reversed number.