- Fork this repo and
git clone
it down to your computer - Create two files
index.html
andscript.js
- Include
script.js
inindex.html
using a<script>
tag - Open
index.html
in your web browser and open the console - After answering each section below,
git commit
your work - When you're finished or when time is up, push your work to your remote repo, and file a Pull Request
Follow these steps:
- Create a function that takes a parameter and logs it
- Create a second function that logs 'hi'
- Call the first function, passing in the second function as an argument
- You should see a function reference being logged
- Alter the first function so that it calls its parameter
- If you did these steps correctly, you should get a log of 'hi'
Write your guesses as a comment in script.js
. Run the code after you guess to see if you were correct.
-
Guess what this will log:
const foo = (param, param2) => { param(param2) } const bar = (param) => { console.log(param) } foo(bar, 'hi')
-
Guess what this will log:
const foo = (param1, param2) => { param1(param2) } const baz = (param) => { console.log(param.toUpperCase()) } foo(baz, 'hello')
-
Guess what this will log:
const foo = (param, param2) => { param(param2, 'hello') } const bar = (param, param2) => { console.log(param2) } foo(bar, 'hi')
A callback is like an electric mixer attachment. Each mixer attachment does something different: slice, dice, spiralise, and all sorts of fancy things depending on the attachment.
The electric mixer also does something: it uses the mixer attachment.
-
Write a function
electricMixer
that takes one parameter namedattachment
. TheelectricMixer
function should console log"This mixer is now: _____"
plus the return value of the attachment. -
Write at least 3 attachments for the electric mixer. Each attachment is a function that returns what it's doing.
i.e.
const spiraliser = () => { return "spiralising" }
-
Call
electricMixer
3 or more times, passing in each of your attachments as arguments.
Example:
electricMixer(spiraliser)
electricMixer(dicer)
electricMixer(grinder)
// Console should show:
// "This mixer is now: spiralising"
// "This mixer is now: dicing"
// "This mixer is now: grinding meat"