JavaScript questions for loop
Question 1:
**Q: Write a for loop that prints numbers from 1 to 5 in the console.**
for(let i=1; i<=5; i++){
[Link](i)
Question 2:
**Q: Using a while loop, print even numbers from 2 to 10 (inclusive) in the
console.**
let i=2;
while(i<=10){
[Link](i)
i+=2;
Question 3:
**Q: Write a for loop to calculate the sum of numbers from 1 to 10.**
let sum=1;
for(let i=1; i<=10; i++){
sum+=i;
}[Link](sum)
Question 4:
**Q: Using a while loop, find the factorial of a given number (e.g., 5).**
let factorial=5;
let find=1;
while(factorial>=1){
find *= factorial;
factorial--;
}[Link](find)
Question 5:
**Q: Write a for loop that prints the square of numbers from 1 to 5.**
for(let i=2; i<=5; i++){
[Link](i*i)
Question 6:
**Q: Using a while loop, print the cube of numbers from 1 to 5.**
let i=1;
while(i<=5){
[Link](i*i*i)
i++;
Question 7:
**Q: Write a for loop to iterate through an array of names and print each name
in the console.**
let array = ["Sanjeev", "Mayank", "Sandesh"]
for(let i=0; i<[Link]; i++){
[Link](array[i])
Question 8:
**Q: Using a while loop, find the sum of elements in an array of numbers.**
let numbers = [2,3,4,5]
let sum =0;
let index =0;
while(index<[Link]){
sum = sum+numbers[index];
index++;
[Link](sum);
Question 9:
**Q: Write a for loop that prints the elements of an array in reverse order.**
let array = [1,2,3,4,5,6,7,8,9]
for(let i= [Link]-1; i>=0; i--){
[Link](array[i])
Question 10:
**Q: Using a while loop, find the largest number in an array of numbers.**
let array = [5,8,9,10,44,33]
largestNumber = 0;
let i = [Link]-1
while(i>=0){
if(array[i]>largestNumber){
largestNumber=array[i];
i--
[Link](largestNumber);
Question 11:
**Q: Write a for loop to calculate the sum of all even numbers from 1 to 20.**
let sum = 0;
for (let i = 2; i <= 20; i += 2) {
sum += i;
[Link](sum);
Question 12:
**Q: Using a while loop, find the product of all odd numbers from 1 to 15.**
let prod = 1;
let num = 1;
while(num <= 15){
if(num % 2 !== 0){
prod *= num;
num++;
[Link](prod);
Question 13:
**Q: Write a for loop to print the following pattern:**
```
*
**
***
****
*****
```
for (let i = 1; i <= 5; i++) {
let pattern = '';
for (let j = 1; j <= i; j++) {
pattern += '*';
[Link](pattern);
Question 14:
**Q: Using a while loop, print the numbers from 10 to 1 in descending order.**
let i = 10;
while (i >= 1) {
[Link](i);
i--;
Question 15:
**Q: Write a for loop to calculate the factorial of a given number (e.g., 6).**
let factorial = 6;
let find = 1;
for (let i = factorial; i >= 1; i--) {
find *= i;
[Link](find);
Question 16:
**Q: Using a while loop, find the sum of digits of a given number (e.g., 123).**
let num = 123;
let string = [Link]();
let sum = 0;
for(let i = 0; i < [Link];i++){
sum += parseInt(string[i]);
[Link](sum);
Question 17:
**Q: Write a for loop to print the following pattern:**
```
55555
4444
333
22
1
```
for (let i = 5; i >= 1; i--) {
let line = "";
for (let j = 0; j < i; j++) {
line += i;
[Link](line);
}
Question 18:
**Q: Using a while loop, print the first 10 Fibonacci numbers.**
let a = 0, b = 1;
[Link](a);
[Link](b);
let count = 2;
while (count < 10) {
let temp = b;
b = a + b;
a = temp;
count++;
[Link](b);
Question 19:
**Q: Write a for loop to find and print the prime numbers between 1 and 20.**
var primes = [];
for(var n=3;n<=20;n+=2) {
if([Link](function(prime){return n%prime!=0})) {
[Link](n);
[Link](2);
[Link](primes)
Question 20:
**Q: Using a while loop, reverse a given number (e.g., 12345).**
let number = 12345;
let result = 0;
while(number>0){
rev = number%10;
result = result*10 + rev;
number = [Link](number/10);
[Link](result)