JavaScript Notes
Practice JavaScript control flow with 20+ programs covering if-else, loops, switch, break, and continue. Each exercise has a clear problem statement, solution, and output.
This chapter contains 20+ practice programs covering all control flow topics: if-else, switch, for loop, while loop, do-while, break, and continue. Each program has a clear problem statement, solution code, and expected output.
Work through these to solidify your understanding!
Program 2: Positive, Negative, or Zero
let n = -5;
if (n > 0) {
console.log(n + " is Positive");
} else if (n < 0) {
console.log(n + " is Negative");
} else {
console.log("The number is Zero");
}-5 is Negative
Program 3: Largest of Three Numbers
let a = 45, b = 78, c = 62;
let largest;
if (a >= b && a >= c) {
largest = a;
} else if (b >= a && b >= c) {
largest = b;
} else {
largest = c;
}
console.log(`Numbers: ${a}, ${b}, ${c}`);
console.log("Largest:", largest);Numbers: 45, 78, 62 Largest: 78
Program 4: Factorial Using For Loop
6! = 720
Program 5: Fibonacci Series (First N Terms)
First 10 Fibonacci numbers: 0 1 1 2 3 5 8 13 21 34
Program 6: Prime Number Check
29 is a PRIME number
Program 7: Sum of Digits
let num = 9876;
let sum = 0;
let temp = num;
while (temp > 0) {
sum += temp % 10;
temp = Math.floor(temp / 10);
}
console.log(`Sum of digits of ${num} = ${sum}`);Sum of digits of 9876 = 30
Program 8: Reverse a Number
let original = 54321;
let reversed = 0;
let temp = original;
while (temp > 0) {
reversed = reversed * 10 + (temp % 10);
temp = Math.floor(temp / 10);
}
console.log("Original:", original);
console.log("Reversed:", reversed);Original: 54321 Reversed: 12345
Program 9: Armstrong Number Check
An Armstrong number equals the sum of its own digits each raised to the power of the number of digits.
let num = 153;
let temp = num;
let sum = 0;
let digits = String(num).length;
while (temp > 0) {
let digit = temp % 10;
sum += Math.pow(digit, digits);
temp = Math.floor(temp / 10);
}
if (sum === num) {
console.log(num + " is an Armstrong number ✓");
console.log("Proof: 1³ + 5³ + 3³ =", 1 + 125 + 27);
} else {
console.log(num + " is NOT an Armstrong number");
}153 is an Armstrong number ✓ Proof: 1³ + 5³ + 3³ = 153
Program 10: Palindrome Number Check
let num = 12321;
let original = num;
let reversed = 0;
let temp = num;
while (temp > 0) {
reversed = reversed * 10 + (temp % 10);
temp = Math.floor(temp / 10);
}
if (original === reversed) {
console.log(original + " is a PALINDROME ✓");
} else {
console.log(original + " is NOT a palindrome");
}12321 is a PALINDROME ✓
Program 11: Leap Year Check
let year = 2024;
if (year % 400 === 0) {
console.log(year + " is a leap year (divisible by 400)");
} else if (year % 100 === 0) {
console.log(year + " is NOT a leap year (divisible by 100, not 400)");
} else if (year % 4 === 0) {
console.log(year + " is a leap year (divisible by 4)");
} else {
console.log(year + " is NOT a leap year");
}2024 is a leap year (divisible by 4)
📐 Pattern Programs
Program 12: Right Triangle Star Pattern
* * * * * * * * * * * * * * *
Program 13: Inverted Triangle
* * * * * * * * * * * * * * *
Program 14: Number Pattern (Floyd's Triangle)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Program 15: Multiplication Table
--- Multiplication Table of 7 --- 7 × 1 = 7 7 × 2 = 14 7 × 3 = 21 7 × 4 = 28 7 × 5 = 35 7 × 6 = 42 7 × 7 = 49 7 × 8 = 56 7 × 9 = 63 7 × 10 = 70
🔤 String Programs
Program 16: Palindrome String Check
"radar" → PALINDROME ✓ "hello" → not palindrome "madam" → PALINDROME ✓ "javascript" → not palindrome "level" → PALINDROME ✓
Program 17: Count Vowels and Consonants
String: "Hello World" Vowels: 3 Consonants: 7
🔀 Break and Continue Programs
Program 18: Find First Element Greater Than 50
First number > 50: 56 (at index 3)
Program 19: Print All Odd Numbers Using Continue
Odd numbers from 1 to 20: 1 3 5 7 9 11 13 15 17 19
🔄 Do-While Programs
Program 20: Guess the Number (Simulation)
Attempt 1: Guessed 10 → Too low! Attempt 2: Guessed 60 → Too high! Attempt 3: Guessed 42 → Correct! 🎉 Found in 3 attempts
🧩 Challenge Programs
Challenge 1: Find All Prime Numbers Up to N
Prime numbers up to 30: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 Count: 10
Challenge 2: Grade Calculator for Multiple Students
Student Results: ──────────────────────────── Alice | 92 | A+ | PASS Bob | 75 | B | PASS Charlie | 58 | C | PASS Diana | 88 | A | PASS Eve | 45 | F | FAIL
Challenge 3: FizzBuzz (Classic Interview Program)
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz
🎤 Interview Questions
Q1. What is FizzBuzz and why is it asked in interviews? > FizzBuzz tests basic programming logic, loop understanding, and condition handling. Print numbers 1 to N, replacing multiples of 3 with "Fizz", multiples of 5 with "Buzz", and multiples of both with "FizzBuzz".
Q2. How do you find if a number is prime? > Check if any number from 2 to √n divides n evenly. If none does, it's prime. Checking only up to √n is an optimization — if n has a factor larger than √n, it must also have one smaller than √n.
Q3. What is an Armstrong number? > A number where the sum of its digits raised to the power of the number of digits equals the number itself. Example: 153 = 1³ + 5³ + 3³ = 1 + 125 + 27 = 153.
Q4. How do you check if a string is a palindrome? > Compare characters from both ends toward the middle. If all pairs match, it's a palindrome. Alternatively, reverse the string and check if it equals the original.
Q5. Write a program to print the first 10 Fibonacci numbers. > Start with 0 and 1, then each subsequent number is the sum of the previous two: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.
🔑 Key Takeaways
| Topic | Programs to Master |
|---|---|
| if-else | Even/odd, positive/negative, grade calculator |
| Nested if | Loan check, multi-criteria validation |
| switch | Calculator, day/month, role-based logic |
| for loop | Factorial, Fibonacci, patterns, multiplication table |
| while loop | Digit sum, reverse, GCD |
| do-while | Menu, input validation, game loop |
| break | Search (find first), stop on condition |
| continue | Filter (skip certain values), data cleaning |
💡 Practice tip: Try to solve each program on your own first, then check the solution. The struggle is where learning happens!
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Control Flow Practice Programs – 20+ Exercises with Solutions.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, control, flow, practice
Related JavaScript Master Course Topics