for loop basic practice test for beeginer
1. Print numbers from 1 to 10:
for (let i = 1; i <= 10; i++) {
console.log(i);
}
2. Print even numbers from 2 to 20:
for (let i = 2; i <= 20; i += 2) { console.log(i); }
3. Calculate the sum of numbers from 1 to 100:
4. Print the multiplication table of 7:
5. Check if a number is even or odd:
let number = 15; if (number % 2 === 0) { console.log(number + " is even."); } else { console.log(number + " is odd."); }
6. Print a pattern of stars:
for (let i = 1; i <= 5; i++) { let stars = ""; for (let j = 1; j <= i; j++) { stars += "*"; } console.log(stars); }
7. Find the factorial of a number:
function factorial(number) { let result = 1; for (let i = 1; i <= number; i++) { result *= i; } return result; } console.log("Factorial of 5 is:", factorial(5));
8. Reverse a string:
let str = "hello"; let reversed = ""; for (let i = str.length - 1; i >= 0; i--) { reversed += str[i]; } console.log("Reversed string:", reversed);
Comments
Post a Comment