Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

JavaScript Questions for Loop

The document contains a series of JavaScript programming questions and their corresponding solutions, focusing on loops such as for and while. It includes tasks like printing numbers, calculating sums and factorials, and working with arrays. Each question is followed by a code snippet demonstrating the solution.

Uploaded by

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

JavaScript Questions for Loop

The document contains a series of JavaScript programming questions and their corresponding solutions, focusing on loops such as for and while. It includes tasks like printing numbers, calculating sums and factorials, and working with arrays. Each question is followed by a code snippet demonstrating the solution.

Uploaded by

xanjv.exotrac
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

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;

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

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"]

for(let i=0; i<array.length; i++){

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]

let sum =0;

let index =0;

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]

for(let i= array.length-1; i>=0; i--){

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;

for (let i = 2; i <= 20; i += 2) {

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;

while(num <= 15){

if(num % 2 !== 0){

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++) {

let pattern = '';

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

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;

for (let i = factorial; i >= 1; i--) {

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 string = num.toString();

let sum = 0;

for(let i = 0; i < string.length;i++){

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--) {

let line = "";

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

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;

while (count < 10) {

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;

result = result*10 + rev;

number = Math.floor(number/10);

console.log(result)

You might also like