
Practice 100 JavaScript Loops Programming Questions
Q1. Write a JavaScript program to print numbers from 1 to 10 using a loop.
Input: None
Expected Output: 1 2 3 4 5 6 7 8 9 10
Q2. Write a JavaScript program to print the first 5 multiples of a number using a loop.
Input: printMultiples(3)
Expected Output: 3 6 9 12 15
Q3. Write a JavaScript program to calculate the sum of numbers from 1 to 10 using a loop.
Input: None
Expected Output: 55
Q4. Write a JavaScript program to print all even numbers between 1 and 20 using a loop.
Input: None
Expected Output: 2 4 6 8 10 12 14 16 18 20
Q5. Write a JavaScript program to print all odd numbers between 1 and 20 using a loop.
Input: None
Expected Output: 1 3 5 7 9 11 13 15 17 19
Q6. Write a JavaScript program to calculate the factorial of a number using a loop.
Input: factorial(5)
Expected Output: 120
Q7. Write a JavaScript program to print the first 10 Fibonacci numbers using a loop.
Input: None
Expected Output: 0 1 1 2 3 5 8 13 21 34
Q8. Write a JavaScript program to print the multiplication table of a number using a loop.
Input: printTable(5)
Expected Output:
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50
Q9. Write a JavaScript program to count the number of digits in a number using a loop.
Input: countDigits(12345)
Expected Output: 5
Q10. Write a JavaScript program to reverse a number using a loop.
Input: reverseNumber(12345)
Expected Output: 54321
Q11. Write a JavaScript program to print the sum of all even numbers between 1 and 100 using a loop.
Input: None
Expected Output: 2550
Q12. Write a JavaScript program to print the sum of all odd numbers between 1 and 100 using a loop.
Input: None
Expected Output: 2500
Q13. Write a JavaScript program to find the largest digit in a number using a loop.
Input: findLargestDigit(9871)
Expected Output: 9
Q14. Write a JavaScript program to print the reverse of a string using a loop.
Input: reverseString("hello")
Expected Output: "olleh"
Q15. Write a JavaScript program to check if a number is a palindrome using a loop.
Input: isPalindrome(121)
Expected Output: "True"
Input: isPalindrome(123)
Expected Output: "False"
Q16. Write a JavaScript program to print all prime numbers between 1 and 50 using a loop.
Input: None
Expected Output: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Q17. Write a JavaScript program to find the power of a number using a loop.
Input: power(2, 3)
Expected Output: 8
Q18. Write a JavaScript program to print all characters of a string one by one using a loop.
Input: printChars("hello")
Expected Output:
h
e
l
l
o
Q19. Write a JavaScript program to calculate the sum of digits of a number using a loop.
Input: sumDigits(12345)
Expected Output: 15
Q20. Write a JavaScript program to find the product of digits of a number using a loop.
Input: productDigits(1234)
Expected Output: 24
Q21. Write a JavaScript program to print the ASCII values of all characters in a string using a loop.
Input: printAscii("ABC")
Expected Output: 65 66 67
Q22. Write a JavaScript program to count the number of vowels in a string using a loop.
Input: countVowels("hello world")
Expected Output: 3
Q23. Write a JavaScript program to check if an array is sorted in ascending order using a loop.
Input: isSorted([1, 2, 3, 4, 5])
Expected Output: "True"
Input: isSorted([5, 3, 1])
Expected Output: "False"
Q24. Write a JavaScript program to print all elements of an array using a loop.
Input: printArray([1, 2, 3])
Expected Output:
1
2
3
Q25. Write a JavaScript program to calculate the sum of all elements in an array using a loop.
Input: sumArray([1, 2, 3])
Expected Output: 6
Q26. Write a JavaScript program to find the smallest number in an array using a loop.
Input: findSmallest([2, 5, 1, 8, 4])
Expected Output: 1
Q27. Write a JavaScript program to find the sum of even numbers in an array using a loop.
Input: sumEvenNumbers([1, 2, 3, 4, 5])
Expected Output: 6
Q28. Write a JavaScript program to print the square of each number in an array using a loop.
Input: printSquares([1, 2, 3, 4])
Expected Output: 1 4 9 16
Q29. Write a JavaScript program to find the largest number in an array using a loop.
Input: findLargest([1, 2, 3, 4])
Expected Output: 4
Q30. Write a JavaScript program to remove duplicates from an array using a loop.
Input: removeDuplicates([1, 2, 2, 3, 4, 4, 5])
Expected Output: [1, 2, 3, 4, 5]
Q31. Write a JavaScript program to check if an array contains a specific number using a loop.
Input: containsNumber([1, 2, 3], 2)
Expected Output: "True"
Input: containsNumber([1, 2, 3], 5)
Expected Output: "False"
Q32. Write a JavaScript program to find the index of the largest number in an array using a loop.
Input: findIndexLargest([1, 5, 3, 9, 2])
Expected Output: 3
Q33. Write a JavaScript program to find the sum of odd numbers in an array using a loop.
Input: sumOddNumbers([1, 2, 3, 4, 5])
Expected Output: 9
Q34. Write a JavaScript program to calculate the product of all elements in an array using a loop.
Input: productArray([1, 2, 3, 4])
Expected Output: 24
Q35. Write a JavaScript program to count how many times a specific number appears in an array using a loop.
Input: countOccurrences([1, 2, 3, 1, 4], 1)
Expected Output: 2
Q36. Write a JavaScript program to check if an array is symmetric using a loop.
Input: isSymmetric([1, 2, 3, 2, 1])
Expected Output: "True"
Input: isSymmetric([1, 2, 3, 4, 5])
Expected Output: "False"
Q37. Write a JavaScript program to reverse an array using a loop.
Input: reverseArray([1, 2, 3])
Expected Output: [3, 2, 1]
Q38. Write a JavaScript program to find the average of numbers in an array using a loop.
Input: findAverage([1, 2, 3, 4])
Expected Output: 2.5
Q39. Write a JavaScript program to find the length of an array using a loop.
Input: findLength([1, 2, 3, 4])
Expected Output: 4
Q40. Write a JavaScript program to print a countdown from 10 to 1 using a loop.
Input: None
Expected Output:
10
9
8
7
6
5
4
3
2
1
Q41. Write a JavaScript program to print the first 10 multiples of a number in reverse order using a loop.
Input: reverseMultiples(3)
Expected Output:
30
27
24
21
18
15
12
9
6
3
Q42. Write a JavaScript program to find the sum of squares of numbers from 1 to 10 using a loop.
Input: None
Expected Output: 385
Q43. Write a JavaScript program to print the sum of first N natural numbers using a loop.
Input: sumNaturalNumbers(5)
Expected Output: 15
Q44. Write a JavaScript program to print all powers of 2 less than 100 using a loop.
Input: None
Expected Output: 1 2 4 8 16 32 64
Q45. Write a JavaScript program to print all perfect numbers between 1 and 1000 using a loop.
Input: None
Expected Output: 6 28 496
Q46. Write a JavaScript program to find the GCD of two numbers using a loop.
Input: findGCD(56, 98)
Expected Output: 14
Q47. Write a JavaScript program to find the LCM of two numbers using a loop.
Input: findLCM(4, 6)
Expected Output: 12
Q48. Write a JavaScript program to find the sum of numbers divisible by 3 and 5 between 1 and 100 using a loop.
Input: None
Expected Output: 2418
Q49. Write a JavaScript program to print the sum of digits of numbers from 1 to 100 using a loop.
Input: None
Expected Output: 855
Q50. Write a JavaScript program to print the multiplication table of a number in reverse order using a loop.
Input: reverseTable(5)
Expected Output:
5 x 10 = 50
5 x 9 = 45
...
5 x 1 = 5
Q51. Write a JavaScript program to find the number of even numbers in an array using a loop.
Input: countEvenNumbers([1, 2, 3, 4, 5])
Expected Output: 2
Q52. Write a JavaScript program to print all prime numbers less than 100 using a loop.
Input: None
Expected Output: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Q53. Write a JavaScript program to check if a number is a perfect square using a loop.
Input: isPerfectSquare(16)
Expected Output: "True"
Input: isPerfectSquare(20)
Expected Output: "False"
Q54. Write a JavaScript program to print numbers from 1 to 100 and print "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of both using a loop.
Input: None
Expected Output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
...
FizzBuzz
Q55. Write a JavaScript program to find the sum of squares of odd numbers in an array using a loop.
Input: sumSquaresOdd([1, 2, 3, 4, 5])
Expected Output: 35
Q56. Write a JavaScript program to print the first 10 terms of an arithmetic sequence using a loop.
Input: printArithmeticSequence(2, 5)
Expected Output: 2 7 12 17 22 27 32 37 42 47
Q57. Write a JavaScript program to find the sum of the first N odd numbers using a loop.
Input: sumOdd(5)
Expected Output: 25
Q58. Write a JavaScript program to print a square pattern of stars.
Input: printStarSquare(4)
Expected Output:
* * * *
* * * *
* * * *
* * * *
Q59. Write a JavaScript program to print a right-angle triangle pattern of stars.
Input: printStarTriangle(4)
Expected Output:
*
* *
* * *
* * * *
Q60. Write a JavaScript program to check if a string is a palindrome using a loop.
Input: isPalindrome("racecar")
Expected Output: "True"
Input: isPalindrome("hello")
Expected Output: "False"
Q61. Write a JavaScript program to print the sum of squares of the first N numbers using a loop.
Input: sumOfSquares(3)
Expected Output: 14
Q62. Write a JavaScript program to print the sum of numbers in the Fibonacci sequence up to a given number using a loop.
Input: sumFibonacci(10)
Expected Output: 88
Q63. Write a JavaScript program to print a triangle of numbers.
Input: printNumberTriangle(4)
Expected Output:
1
1 2
1 2 3
1 2 3 4
Q64. Write a JavaScript program to print a pyramid pattern of stars.
Input: printPyramid(4)
Expected Output:
*
* *
* * *
* * * *
Q65. Write a JavaScript program to find the sum of even-indexed elements in an array using a loop.
Input: sumEvenIndexed([1, 2, 3, 4, 5])
Expected Output: 9
Q66. Write a JavaScript program to print the sum of prime numbers in an array using a loop.
Input: sumPrimes([1, 2, 3, 4, 5, 6, 7])
Expected Output: 17
Q67. Write a JavaScript program to find the difference between the largest and smallest number in an array using a loop.
Input: findDifference([3, 5, 1, 8, 7])
Expected Output: 7
Q68. Write a JavaScript program to print all powers of 3 less than 100 using a loop.
Input: None
Expected Output: 1 3 9 27 81
69. Write a JavaScript program to find the sum of prime numbers between 1 and N using a loop.
Input: sumPrimes(10)
Expected Output: 17
Q70. Write a JavaScript program to print the multiplication table of a number in reverse order, starting from 10 down to 1.
Input: reverseTable(5)
Expected Output:
5 x 10 = 50
5 x 9 = 45
...
5 x 1 = 5
Q71. Write a JavaScript program to find the number of vowels in an array of strings using a loop.
Input: countVowelsInArray(["hello", "world"])
Expected Output: 3
Q72. Write a JavaScript program to print the elements of an array in reverse order using a loop.
Input: reverseArray([1, 2, 3, 4])
Expected Output: [4, 3, 2, 1]
Q73. Write a JavaScript program to print the sum of squares of even numbers in an array using a loop.
Input: sumSquareEven([1, 2, 3, 4, 5])
Expected Output: 20
Q74. Write a JavaScript program to find the number of occurrences of each element in an array using a loop.
Input: countOccurrences([1, 2, 2, 3, 4, 4, 4])
Expected Output: {1: 1, 2: 2, 3: 1, 4: 3}
Q75. Write a JavaScript program to print the first N even numbers using a loop.
Input: printEvenNumbers(5)
Expected Output: 2 4 6 8 10
Q76. Write a JavaScript program to calculate the sum of numbers divisible by 2 and 7 between 1 and 100 using a loop.
Input: None
Expected Output: 602
Q77. Write a JavaScript program to find the average of the first N natural numbers using a loop.
Input: averageNaturalNumbers(5)
Expected Output: 3
Q78. Write a JavaScript program to print a sequence of numbers with a difference of 2 using a loop.
Input: printSequence(5)
Expected Output: 1 3 5 7 9
Q79. Write a JavaScript program to print a diamond pattern of stars.
Input: printDiamond(3)
Expected Output:
*
* *
* * *
* *
*
Q80. Write a JavaScript program to calculate the sum of elements of a 2D array using a loop.
Input: sum2DArray([[1, 2], [3, 4], [5, 6]])
Expected Output: 21
Q81. Write a JavaScript program to print numbers from 1 to 100, but print "Buzz" for multiples of 5, "Fizz" for multiples of 3, and "FizzBuzz" for multiples of both using a loop.
Input: None
Expected Output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
...
FizzBuzz
Q82. Write a JavaScript program to print all numbers between 1 and N that are divisible by 7 using a loop.
Input: divisibleBy7(50)
Expected Output: 7 14 21 28 35 42 49
Q83. Write a JavaScript program to find the second largest number in an array using a loop.
Input: secondLargest([1, 2, 3, 4, 5])
Expected Output: 4
Q84. Write a JavaScript program to find the total number of characters in an array of strings using a loop.
Input: countTotalChars(["hello", "world"])
Expected Output: 10
Q85. Write a JavaScript program to check if a number is divisible by both 3 and 5 using a loop.
Input: isDivisibleBy3And5(15)
Expected Output: "True"
Input: isDivisibleBy3And5(10)
Expected Output: "False"
Q86. Write a JavaScript program to calculate the sum of multiples of a given number up to N using a loop.
Input: sumMultiples(3, 10)
Expected Output: 30
Q87. Write a JavaScript program to find the median of an array of numbers using a loop.
Input: findMedian([1, 2, 3, 4, 5])
Expected Output: 3
Q88. Write a JavaScript program to print a right-angled triangle pattern of numbers.
Input: printRightAngleTriangle(4)
Expected Output:
1
1 2
1 2 3
1 2 3 4
Q89. Write a JavaScript program to print all even numbers in a 2D array using a loop.
Input: printEvenNumbers2D([[1, 2], [3, 4], [5, 6]])
Expected Output: 2 4 6
Q90. Write a JavaScript program to find the number of prime numbers in an array using a loop.
Input: countPrimes([1, 2, 3, 4, 5])
Expected Output: 3
Q91. Write a JavaScript program to find the sum of cubes of numbers from 1 to N using a loop.
Input: sumCubes(3)
Expected Output: 36
Q92. Write a JavaScript program to find the sum of positive numbers in an array using a loop.
Input: sumPositive([1, -2, 3, -4, 5])
Expected Output: 9
Q93. Write a JavaScript program to print a list of all divisors of a number using a loop.
Input: printDivisors(6)
Expected Output: 1 2 3 6
Q94. Write a JavaScript program to print the numbers from 1 to N that are divisible by both 2 and 3 using a loop.
Input: divisibleBy2And3(20)
Expected Output: 6 12 18
Q95. Write a JavaScript program to calculate the sum of squares of all even numbers in a 2D array using a loop.
Input: sumSquaresEven2D([[1, 2], [3, 4], [5, 6]])
Expected Output: 56
Q96. Write a JavaScript program to print the sum of digits of numbers from 1 to N using a loop.
Input: sumDigitsUpToN(10)
Expected Output: 46
Q97. Write a JavaScript program to print all powers of 5 up to a given number using a loop.
Input: powersOf5(100)
Expected Output: 1 5 25
Q98. Write a JavaScript program to calculate the product of all odd numbers in an array using a loop.
Input: productOdd([1, 2, 3, 4, 5])
Expected Output: 15
Q99. Write a JavaScript program to check if a number is divisible by either 2 or 3 using a loop.
Input: isDivisibleBy2or3(6)
Expected Output: "True"
Input: isDivisibleBy2or3(7)
Expected Output: "False"
Q100. Write a JavaScript program to print the sum of multiples of 5 up to 100 using a loop.
Input: None
Expected Output: 1050