Control Flow - Practice Exercises
Topic: Control Flow
Concepts:if,else,switch, conditional statements
📝 Multiple Choice Questions
Section titled “📝 Multiple Choice Questions”Question 1
Section titled “Question 1”What will this code output?
let x = 10;if (x > 5) { console.log("A");} else if (x > 15) { console.log("B");} else { console.log("C");}- A) “A”
- B) “B”
- C) “C”
- D) Nothing
Question 2
Section titled “Question 2”Which of these values is considered “falsy” in JavaScript?
- A) “false”
- B) []
- C) 0
- D) ” “
Question 3
Section titled “Question 3”What’s the output of this switch statement?
let day = "Monday";switch (day) { case "Monday": console.log("Start"); case "Tuesday": console.log("Continue"); break; default: console.log("Default");}- A) “Start”
- B) “Start” then “Continue”
- C) “Start” then “Continue” then “Default”
- D) Error
Question 4
Section titled “Question 4”What does the ternary operator condition ? value1 : value2 do?
- A) Returns value1 if condition is false
- B) Returns value2 if condition is true
- C) Returns value1 if condition is true, value2 if false
- D) Always returns value1
Question 5
Section titled “Question 5”Which comparison operator checks both value AND type?
- A) ==
- B) ===
- C) !=
- D) <>
💻 Practical Coding Exercises
Section titled “💻 Practical Coding Exercises”Exercise 1: Grade Calculator
Section titled “Exercise 1: Grade Calculator”Create a program that converts numeric scores to letter grades:
// TODO: Create a variable for a student's score (0-100)// TODO: Use if-else statements to assign letter grades:// A: 90-100, B: 80-89, C: 70-79, D: 60-69, F: below 60// TODO: Display the score and corresponding letter grade
// HINT: Use if-else if-else chain// HINT: Start with the highest grade first// HINT: Use >= for comparisons// HINT: Display both the numeric score and letter gradeExercise 2: Traffic Light System
Section titled “Exercise 2: Traffic Light System”Create a traffic light decision system:
// TODO: Create a variable for traffic light color// TODO: Use switch statement to determine action:// Red: "STOP", Yellow: "CAUTION", Green: "GO", Invalid: "INVALID COLOR"// TODO: Display the action message
// HINT: Use switch statement// HINT: Convert color to lowercase for consistency// HINT: Use default case for invalid colors// HINT: Don't forget break statementsExercise 3: Age Category Classifier
Section titled “Exercise 3: Age Category Classifier”Classify people by age groups:
// TODO: Create a variable for person's age// TODO: Categorize into age groups:// 0-12: "Child", 13-19: "Teenager", 20-64: "Adult", 65+: "Senior"// TODO: Handle invalid ages (negative numbers)// TODO: Display the age category
// HINT: Use if-else if-else chain// HINT: Check for negative ages first// HINT: Use appropriate comparison operators for ranges// HINT: Consider inclusive vs exclusive rangesExercise 4: Day Type Checker
Section titled “Exercise 4: Day Type Checker”Check if a day is a weekday or weekend:
// TODO: Create a variable for a day of the week// TODO: Use switch statement to determine if it's a weekday or weekend// TODO: Display the result
// HINT: Group multiple cases together// HINT: Monday-Friday are weekdays, Saturday-Sunday are weekends// HINT: Handle case-insensitive input// HINT: Include a default case for invalid daysExercise 5: Temperature Advisor
Section titled “Exercise 5: Temperature Advisor”Give clothing advice based on temperature:
// TODO: Create a variable for temperature in Fahrenheit// TODO: Give advice based on temperature:// Below 32: "Wear a heavy coat!"// 32-50: "Wear a jacket"// 51-70: "Wear a sweater"// 71-80: "Perfect weather!"// Above 80: "Stay cool!"// TODO: Display the temperature and advice
// HINT: Use if-else if-else chain// HINT: Use appropriate comparison operators// HINT: Consider the order of conditionsChallenge Problems
Section titled “Challenge Problems”Challenge 1: Grade Calculator with Comments
Section titled “Challenge 1: Grade Calculator with Comments”Create a comprehensive grade calculator:
// TODO: Create a variable for a student's score (0-100)// TODO: Calculate letter grade and add comments:// A (90-100): "Excellent work!"// B (80-89): "Good job!"// C (70-79): "Fair work"// D (60-69): "Needs improvement"// F (below 60): "Must retake"// TODO: Also check for invalid scores (negative or over 100)
// HINT: Use if-else if-else chain// HINT: Validate input first// HINT: Display both grade and commentChallenge 2: Simple Calculator
Section titled “Challenge 2: Simple Calculator”Create a basic calculator using conditional statements:
// TODO: Create variables for two numbers and an operator (+, -, *, /)// TODO: Use switch statement to perform the calculation// TODO: Handle division by zero// TODO: Handle invalid operators// TODO: Display the calculation and result
// HINT: Use switch statement for operators// HINT: Check for division by zero before calculating// HINT: Use default case for invalid operators// HINT: Format output nicely (e.g., "5 + 3 = 8")Challenge 3: Voting Eligibility Checker
Section titled “Challenge 3: Voting Eligibility Checker”Check if someone can vote based on multiple criteria:
// TODO: Create variables for age, citizenship status, and registration status// TODO: Check eligibility (must be 18+, citizen, and registered)// TODO: Provide specific feedback for why someone can't vote// TODO: Display the eligibility result
// HINT: Use logical operators (&&, ||)// HINT: Check each condition and provide specific feedback// HINT: Consider using nested if statements// HINT: Give encouraging messages for eligible votersDebugging Exercises
Section titled “Debugging Exercises”Debug Exercise 1: Missing Break Statement
Section titled “Debug Exercise 1: Missing Break Statement”Find and fix the error:
// This switch statement has a buglet day = "Monday";let dayType;
switch (day) { case "Monday": case "Tuesday": case "Wednesday": case "Thursday": case "Friday": dayType = "Weekday"; case "Saturday": case "Sunday": dayType = "Weekend"; break; default: dayType = "Invalid day";}
console.log(dayType); // What will this output and why?
// TODO: Fix the bug// HINT: What happens when break statements are missing?Debug Exercise 2: Assignment vs Comparison
Section titled “Debug Exercise 2: Assignment vs Comparison”Find and fix the error:
// This condition has a buglet temperature = 75;
if ((temperature = 70)) { console.log("Perfect temperature!");} else { console.log("Not perfect temperature");}
// TODO: Fix the bug// HINT: Look at the comparison operatorDebug Exercise 3: Logic Error
Section titled “Debug Exercise 3: Logic Error”Find and fix the error:
// This age check has a logic errorlet age = 25;let hasLicense = false;
// Should allow driving if 18+ AND has licenseif (age >= 18 || hasLicense) { console.log("Can drive");} else { console.log("Cannot drive");}
// TODO: Fix the logical operator// HINT: Should it be OR or AND?Real-World Applications
Section titled “Real-World Applications”Application 1: ATM Transaction
Section titled “Application 1: ATM Transaction”Simulate ATM transaction logic:
// TODO: Create variables for account balance, withdrawal amount, and daily limit// TODO: Check if transaction is allowed:// - Sufficient funds// - Within daily limit// - Positive withdrawal amount// TODO: Display appropriate messages
// HINT: Use multiple conditions with logical operators// HINT: Check for edge cases (negative amounts, zero balance)// HINT: Provide clear feedback for each conditionApplication 2: Shipping Cost Calculator
Section titled “Application 2: Shipping Cost Calculator”Calculate shipping costs based on weight and distance:
// TODO: Create variables for package weight and shipping distance// TODO: Calculate shipping cost:// - Light package (0-5 lbs): Base rate $5// - Medium package (6-20 lbs): Base rate $10// - Heavy package (21+ lbs): Base rate $20// - Add $2 for every 100 miles over 500 miles// TODO: Display the shipping cost breakdown
// HINT: Use if-else for weight categories// HINT: Calculate distance surcharge separately// HINT: Show detailed cost breakdownApplication 3: Movie Theater Pricing
Section titled “Application 3: Movie Theater Pricing”Calculate movie ticket prices with discounts:
// TODO: Create variables for customer age, movie time, and membership status// TODO: Apply pricing rules:// - Base price: $12// - Senior (65+): 25% discount// - Child (under 13): 30% discount// - Matinee (before 5 PM): $3 off// - Member: Additional 10% off final price// TODO: Display original price, discounts applied, and final price
// HINT: Calculate discounts step by step// HINT: Apply membership discount last// HINT: Show all calculations clearlySelf-Assessment Checklist
Section titled “Self-Assessment Checklist”After completing these exercises, check that you can:
- Write basic if-else statements
- Use if-else if-else chains for multiple conditions
- Use switch statements with multiple cases
- Handle default cases in switch statements
- Use logical operators (&&, ||, !) in conditions
- Compare values using ===, !==, >, <, >=, <=
- Handle edge cases and invalid inputs
- Debug common conditional logic errors
- Apply conditional logic to real-world problems
- Write clean, readable conditional code
Additional Practice Suggestions
Section titled “Additional Practice Suggestions”- Create a simple quiz program with multiple choice questions
- Build a basic recommendation system (movie, book, restaurant)
- Make a simple game with branching storylines
- Create a form validator for user input
- Build a basic chatbot with predefined responses
Next Topic: 05 - Loops