Loop Programs
for, while, for...of. Tables, factorials, primes. You will get tired of i++.
6 programs · open one, then the next
Start with Multiplication Table- 01Multiplication TableBeginnerfor (let i = 1; i <= 10; i++). The extra = is the off-by-one.Open example →js// Method 1: Using for loop let number = 5; console.log("Multiplication table of", number + ":");
- 02Sum of n Natural NumbersBeginnern(n+1)/2. A loop also works. / in JS already gives a float.Open example →js// Method 1: Using for loop let n = 10; let sum = 0;
- 03Factorial of a NumberBeginnerMultiply 1 through n. 0! is 1. Negative n is not a number you want here.Open example →js// Factorial: n! = n × (n-1) × (n-2) × ... × 2 × 1 // Example: 5! = 5 × 4 × 3 × 2 × 1 = 120 // Method 1: Using for loop
- 04Reverse a NumberBeginnerPeel % 10, glue onto result * 10. 100 becomes 1. Trailing zeros die.Open example →js// Method 1: Using while loop function reverseNumber(num) { let reversed = 0; let original = num;
- 05Palindrome CheckBeginnerReverse it. If it matches, palindrome. 10 is not. 121 is.Open example →js// Palindrome: reads same forwards and backwards // Examples: 121, "madam", "racecar" // Method 1: String palindrome
- 06Prime Number CheckIntermediateTrial divide up to sqrt(n). 1 is not prime. 2 is. 9 is not.Open example →js// Prime number: divisible only by 1 and itself // Examples: 2, 3, 5, 7, 11, 13, 17, 19 // Method 1: Basic check