0% found this document useful (0 votes)
7 views3 pages

Javascript For Loop Practice

The document contains a series of JavaScript practice questions and answers focused on using for loops. It includes tasks such as printing numbers in various sequences, calculating sums and factorials, printing multiplication tables, and displaying patterns. Each question is followed by a code snippet demonstrating the solution.

Uploaded by

azaanaqeel24
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Javascript For Loop Practice

The document contains a series of JavaScript practice questions and answers focused on using for loops. It includes tasks such as printing numbers in various sequences, calculating sums and factorials, printing multiplication tables, and displaying patterns. Each question is followed by a code snippet demonstrating the solution.

Uploaded by

azaanaqeel24
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JavaScript For Loop Practice Questions and

Answers

1. Print numbers from 1 to 10

Question:
Print numbers from 1 to 10

Answer:
for (let i = 1; i <= 10; i++) {
[Link](i);
}

2. Print numbers from 10 to 1

Question:
Print numbers from 10 to 1

Answer:
for (let i = 10; i >= 1; i--) {
[Link](i);
}

3. Print even numbers from 1 to 20

Question:
Print even numbers from 1 to 20

Answer:
for (let i = 2; i <= 20; i += 2) {
[Link](i);
}

4. Print odd numbers from 1 to 15

Question:
Print odd numbers from 1 to 15

Answer:
for (let i = 1; i <= 15; i += 2) {
[Link](i);
}
5. Find the sum of numbers from 1 to 10

Question:
Find the sum of numbers from 1 to 10

Answer:
let sum = 0;

for (let i = 1; i <= 10; i++) {


sum = sum + i;
}

[Link](sum);

6. Print multiplication table of 5

Question:
Print multiplication table of 5

Answer:
for (let i = 1; i <= 10; i++) {
[Link]("5 x ", i, " = ", (5 * i));
}

7. Print elements of an array

Question:
Print elements of an array

Answer:
let arr = [10, 20, 30, 40];

for (let i = 0; i < [Link]; i++) {


[Link](arr[i]);
}

8. Count numbers divisible by 3 from 1 to 30

Question:
Count numbers divisible by 3 from 1 to 30

Answer:
let count = 0;

for (let i = 1; i <= 30; i++) {


if (i % 3 === 0) {
count++;
}
}

[Link](count);
9. Find factorial of 5

Question:
Find factorial of 5

Answer:
let fact = 1;

for (let i = 1; i <= 5; i++) {


fact = fact * i;
}

[Link](fact);

10. Print a star pattern

Question:
Print a star pattern

Answer:
for (let i = 1; i <= 5; i++) {
let stars = "";

for (let j = 1; j <= i; j++) {


stars += "*";
}

[Link](stars);
}

You might also like