JavaScript Questions for Loop
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++){
console.log(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){
console.log(i)
i+=2;
Question 3:
**Q: Write a for loop to calculate the sum of numbers from 1 to 10.**
let sum=1;
sum+=i;
}console.log(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--;
}console.log(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++){
console.log(i*i)
Question 6:
**Q: Using a while loop, print the cube of numbers from 1 to 5.**
let i=1;
while(i<=5){
console.log(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"]
console.log(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]
while(index<numbers.length){
sum = sum+numbers[index];
index++;
console.log(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]
console.log(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 = array.length-1
while(i>=0){
if(array[i]>largestNumber){
largestNumber=array[i];
i--
console.log(largestNumber);
Question 11:
**Q: Write a for loop to calculate the sum of all even numbers from 1 to 20.**
let sum = 0;
sum += i;
console.log(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;
prod *= num;
num++;
console.log(prod);
Question 13:
**Q: Write a for loop to print the following pattern:**
```
*
**
***
****
*****
```
for (let i = 1; i <= 5; i++) {
pattern += '*';
console.log(pattern);
Question 14:
**Q: Using a while loop, print the numbers from 10 to 1 in descending order.**
let i = 10;
while (i >= 1) {
console.log(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;
find *= i;
console.log(find);
Question 16:
**Q: Using a while loop, find the sum of digits of a given number (e.g., 123).**
let num = 123;
let sum = 0;
sum += parseInt(string[i]);
console.log(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--) {
line += i;
console.log(line);
}
Question 18:
**Q: Using a while loop, print the first 10 Fibonacci numbers.**
let a = 0, b = 1;
console.log(a);
console.log(b);
let count = 2;
let temp = b;
b = a + b;
a = temp;
count++;
console.log(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(primes.every(function(prime){return n%prime!=0})) {
primes.push(n);
primes.unshift(2);
console.log(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;
number = Math.floor(number/10);
console.log(result)