About Lesson
1. Calculate the Sum of Numbers:
Description: This function calculates the sum of all integers from 1 to 'n'.
function calculateSum(n) {
let sum = 0;
for (let i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
Test Case:
console.log("Sum of numbers from 1 to 5:", calculateSum(5)); // Output: 15
2. Factorial Calculation:
Description: This function computes the factorial of a number 'n', which is the product of all positive integers from 1 to 'n'.
function calculateFactorial(n) {
let factorial = 1;
for (let i = 1; i <= n; i++) {
factorial *= i;
}
return factorial;
}
Test Case:
console.log("Factorial of 4:", calculateFactorial(4)); // Output: 24
3. Print Multiplication Table:
Description: This function prints the multiplication table for a given number 'n'.
function printMultiplicationTable(n) {
for (let i = 1; i <= 10; i++) {
console.log(`${n} x ${i} = ${n * i}`);
}
}
Test Case:
console.log("Multiplication table for 3:");
printMultiplicationTable(3);
4. Reverse a String:
Description: This function reverses a given string.
function reverseString(str) {
return str.split('').reverse().join('');
}
Test Case:
console.log("Reversed string:", reverseString("Hello")); // Output: "olleH"
5. Check Even or Odd:
Description: This function checks if a given number is even or odd.
function isEvenOrOdd(num) {
return num % 2 === 0 ? "Even" : "Odd";
}
Test Case:
console.log("Is 7 even or odd:", isEvenOrOdd(7)); // Output: "Odd"
6. Count Vowels in a String:
Description: This function counts the number of vowels in a given string.
function countVowels(str) {
return str.match(/[aeiouAEIOU]/g).length;
}
Test Case:
console.log("Number of vowels in 'Hello':", countVowels("Hello")); // Output: 2
7. Find the Maximum Number:
Description: This function finds the maximum number from an array of numbers.
function findMax(arr) {
return Math.max(...arr);
}
Test Case:
console.log("Maximum number in [5, 10, 3, 8]:", findMax([5, 10, 3, 8])); // Output: 10
8. Calculate the Square of a Number:
Description: This function calculates the square of a given number.
function squareNumber(num) {
return num * num;
}
Test Case:
console.log("Square of 6:", squareNumber(6)); // Output: 36