Module 5: Functions
Lab: Greeting Function, Factorial, and Timed Message
Practice creating different types of functions: a simple greeting, a recursive factorial, and a timed message using setTimeout
.
Tasks to Complete:
- Create a function `greetUser(name)` that takes a `name` as a parameter and logs a personalized greeting (e.g., "Hello, [name]! Welcome.") to the console. Call this function with your own name.
- Create a recursive function `factorial(n)` that calculates the factorial of a non-negative integer `n`. Remember the base case (factorial of 0 or 1 is 1) and the recursive step (`n * factorial(n-1)`). Log the result of `factorial(5)` to the console.
- Use `setTimeout` to schedule a function that logs the message "This message appears after 3 seconds!" to the console. The message should appear 3000 milliseconds (3 seconds) after the script starts running.
JavaScript Sandbox
Output will appear here...
JavaScript execution is sandboxed. Output from `console.log()` and errors will appear above.