CPNT 262 Day 4 - Function fundamentals
Housekeeping
Goals of the day
- Tip Calculator using functions
- Eat and Drink click handlers
1. Spoiler demo: String methods
2. Defining and invoking functions
Knowledge of functions is a key skill in software development, especially in Javascript. An important concept to understand is that code inside a function is not run when a function is defined; only when the function is invoked.
Materials
- MDN:
- Takeaways: Function terminology
- Gist: Defining functions
- Gist: Invoking functions
Activity
3. Arguments and parameters
A core goal of programming is to write DRY (Do Not Repeat yourself) code. In this session, we will remove repetitive code with the use of functions.
Materials
- MDN:
- Gist: Function arguments and parameters
- Gist: Function return values
Activity
4. Event listeners
Materials
Activity
- Codepen: A simple click handler
- Activity: Eat and Drink click handlers
Prep
Functions
- Read: Functions - reusable blocks of code
- Read: Build your own function
- Read: Function return values
- Read: Function expression
- Watch (by Steve Griffith):
Events and Element.addEventListener()
- Read: Introduction to events
EventTarget.addEventListener()
- Watch: Introduction to JavaScript Event Listeners by Steve Griffith
- Reference: MDN Event Reference
Free Code Camp Exercises
- Javascript basics: Exercises 47-55
- Start: Write Reusable JavaScript with Functions
- Stop: Stand in Line