🏠

💡 JavaScript Conditional Statements Practice Exercises

📑 Table of Contents

Exercise 1: Check if a Number is Positive — ⭐☆☆ (Easy)

Write a function that checks whether a number is positive, negative, or zero.
function checkNumber(num) {
          if (num > 0) {
            return "Positive";
          } else if (num < 0) {
            return "Negative";
          } else {
            return "Zero";
          }
        }

Exercise 2: Even or Odd Number Checker — ⭐☆☆ (Easy)

Write a function that determines if a given number is even or odd.
function isEvenOrOdd(num) {
          if (num % 2 === 0) {
            return "Even";
          } else {
            return "Odd";
          }
        }

Exercise 3: Determine if a Person is Eligible to Vote — ⭐☆☆ (Easy)

Write a function that checks if a person is eligible to vote based on age. A person is eligible if their age is 18 or above.
function canVote(age) {
          if (age >= 18) {
            return "Eligible to vote";
          } else {
            return "Not eligible to vote";
          }
        }

Exercise 4: Grade Calculator — ⭐⭐☆ (Medium)

Create a function that calculates a letter grade based on a numerical score: Implement this using both if/else if and switch statements.
// If/else if version
        function calculateGradeIfElse(score) {
          if (score >= 90 && score <= 100) return 'A';
          else if (score >= 80) return 'B';
          else if (score >= 70) return 'C';
          else if (score >= 60) return 'D';
          else if (score >= 0) return 'F';
          else return 'Invalid score';
        }
        
        // Switch version
        function calculateGradeSwitch(score) {
          const gradeRange = Math.floor(score / 10);
          switch (gradeRange) {
            case 10:
            case 9: return 'A';
            case 8: return 'B';
            case 7: return 'C';
            case 6: return 'D';
            case 5:
            case 4:
            case 3:
            case 2:
            case 1:
            case 0: return 'F';
            default: return 'Invalid score';
          }
        }

Exercise 5: Day of Week Message Generator — ⭐⭐☆ (Medium)

Create a function that takes a day number (1-7) and returns a custom message for each day of the week: Implement this using both if/else if and switch statements.
// If/else if version
        function getDayMessageIfElse(dayNumber) {
          if (dayNumber === 1) return "Start of the work week";
          else if (dayNumber === 2) return "Taco Tuesday!";
          else if (dayNumber === 3) return "Halfway there!";
          else if (dayNumber === 4) return "Almost the weekend";
          else if (dayNumber === 5) return "TGIF!";
          else if (dayNumber === 6) return "Weekend fun day";
          else if (dayNumber === 7) return "Rest day";
          else return "Invalid day number";
        }
        
        // Switch version
        function getDayMessageSwitch(dayNumber) {
          switch (dayNumber) {
            case 1: return "Start of the work week";
            case 2: return "Taco Tuesday!";
            case 3: return "Halfway there!";
            case 4: return "Almost the weekend";
            case 5: return "TGIF!";
            case 6: return "Weekend fun day";
            case 7: return "Rest day";
            default: return "Invalid day number";
          }
        }

Exercise 6: Shopping Cart Discount Calculator — ⭐⭐☆ (Medium)

Create a function called calculateDiscount that determines the discount a customer receives based on their total purchase amount: Then, write both if/else if and switch versions of this function.
// If/else if version
        function calculateDiscountIfElse(totalAmount) {
          if (totalAmount < 50) return 0;
          else if (totalAmount < 100) return totalAmount * 0.05;
          else if (totalAmount < 200) return totalAmount * 0.10;
          else return totalAmount * 0.15;
        }
        
        // Switch version
        function calculateDiscountSwitch(totalAmount) {
          const range = Math.floor(totalAmount / 50);
          switch (range) {
            case 0: return 0;
            case 1: return totalAmount * 0.05;
            case 2:
            case 3: return totalAmount * 0.10;
            default: return totalAmount * 0.15;
          }
        }

Exercise 7: Shipping Cost Calculator — ⭐⭐⭐ (Hard)

Create a function that calculates shipping cost based on: Rules:
function calculateShippingCost(weight, distance, speed) {
          let cost = 0;
          if (weight < 1) cost += weight * 1.50;
          else if (weight <= 5) cost += weight * 3.00;
          else cost += weight * 4.50;
        
          if (distance >= 100 && distance <= 500) cost += 5;
          else if (distance > 500) cost += 10;
        
          switch (speed.toLowerCase()) {
            case "standard": break;
            case "express": cost += 12; break;
            case "overnight": cost += 25; break;
            default: return "Invalid shipping speed";
          }
        
          return cost.toFixed(2);
        }