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:


let sum = 0; for (let i = 1; i <= 100; i++) { sum += i; } console.log("The sum is:", sum);



------------------

4. Print the multiplication table of 7:

for (let i = 1; i <= 12; i++) { console.log(7 * i); }

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

Popular posts from this blog

practice test of Arithmetic Operations: