EXERCISE 3: Functions - Calculator

Assignment

Create a calculator program with functions that:

  1. Has a function called add that takes two numbers as parameters and returns their sum
  2. Has a function called subtract that takes two numbers as parameters and returns their difference
  3. Has a function called multiply that takes two numbers as parameters and returns their product
  4. Has a function called divide that takes two numbers as parameters and returns their quotient (handle division by zero)
  5. Has a function called calculate that takes three parameters: num1, num2, and operation ('add', 'subtract', 'multiply', or 'divide')
  6. The calculate function should use if/else or switch to call the appropriate function
  7. Test your calculator with at least 3 different operations

Requirements:

  • Use function declarations (function keyword)
  • Display results using console.log()
  • Handle division by zero (return "Cannot divide by zero" or similar)
  • Use prompt() to get user input for numbers and operation
  • Use an external JavaScript file (script.js) linked to your HTML

Create an index.html file and a script.js file with your solution.