** JavaScript Functions **
Create a file called functions.js and complete the following exercises. Remember to test your code as you go!
Question 1: Function Basics
Write a JavaScript function named calculateArea that calculates and returns the area of a rectangle. The function should take two parameters: length and width. Test your function by calling it with different values.
Question 2: Scope Exploration
- Declare a variable
globalVarin the global scope and give it a value. - Inside a function, declare a variable
localVarand give it a different value. - Attempt to access both
globalVarandlocalVarwithin the function. - Write comments explaining your observations about variable scope.
Question 3: Closures and Private Counters
Create a function called counter that returns another function. The inner function should increment and return a counter variable each time it's called. Use closures to achieve this. Test your counter function by creating multiple counters and incrementing them separately.
Question 4 & 5 is not required, but is a bonus challenge.
Question 4: Function Expressions
Write a function expression named calculate that takes two parameters: num1 and num2. The function should return an object with the following properties:
sum- the sum ofnum1andnum2.difference- the absolute difference betweennum1andnum2.product- the product ofnum1andnum2.quotient- the result of dividingnum1bynum2.
Question 5: Callback Functions
Create a function called performOperation that takes two numbers and a callback function as parameters. The performOperation function should call the callback function, passing the two numbers as arguments, and return the result.
Write three separate callback functions:
- One that adds the two numbers.
- One that subtracts the second number from the first.
- One that multiplies the numbers.