Callbacks Node.js App: Individual Exercises
Complete the individual exercises to update the Callbacks app!
ASCII Arrow
- Define a new function named
drawArrow
that logs an ASCII arrow to the console:'>>------>'
- Call the
repeat
function to repeat the new function 10 times- Pass the
drawArrow
function as the callback argument - Pass
10
as the second argument
- Pass the
Repeat Two Functions
- Define a new function named
repeatTwo
- The
repeatTwo
function should have three parameters:callback1
,callback2
, andnumberOfTimes
- Copy the body of the existing
repeat
function, and paste it into the body of therepeatTwo
function - Update the body of the
repeatTwo
function so that it calls both thecallback1
andcallback2
functions in thewhile
loop - Finally, call the
repeatTwo
function to repeat thedrawArrow
anddrawChefHat
functions2
times
Repeat with Parameter
Sometimes it is necessary to call callbacks that require parameters. Follow the instructions below to practice.
Defining a New Function with a Parameter
- Define a new function named
exclaim
- The
exclaim
function should have one parameter:message
- In the body of the
exclaim
function, declare a new variable namednewMessage
usinglet
- Assign the
newMessage
variable to be themessage
parameter with an exclamation point (!
) at the end of it- For example, if
message
were "hello",newMessage
should be "hello!"
- For example, if
- Use
console.log
to log thenewMessage
to the console
Defining a New Function to Repeat Functions
- Define a new function named
repeatWithParameter
- The
repeatWithParameter
function should have three parameters:callback
,callbackParameter
, andnumberOfTimes
- Copy the body of the existing
repeat
function, and paste it into the body of therepeatWithParameter
function - Update the body of the
repeatWithParameter
function so that it passes thecallbackParameter
when calling thecallback
function - Finally, call the
repeatWithParameter
function, and make it call theexclaim
function3
times with a message of "hello"
CHALLENGE - Random Function
- Define a new function named
randomFunction
- The
randomFunction
function should have two parameters:callback1
,callback2
- In the body of the
randomFunction
function, declare a variable namedrandomNumber
usinglet
- Use
Math.random()
to generate a random number between 0 and 1, and store the result in therandomNumber
variable - Use an
if
statement to check ifrandomNumber
is less than.5
- If
randomNumber
is less than.5
, call thecallback1
function - Using an
else
, ifrandomNumber
is NOT less than.5
, call thecallback2
function
CHALLENGE - Arrow Length
- Define a new function named
drawArrowLength
- The function should have one parameter:
length
- The function should have one parameter:
- In the body of the
drawArrowLength
function, build a string for the arrow based on thelength
parameter- The number of hyphen characters (
-
) should match thelength
- For example, if
length
were5
, the arrow would be>>----->
- If
length
were2
, the arrow would be>>-->
- The number of hyphen characters (
- Call the
repeatWithParameter
function, and make it call thedrawArrowLength
function3
times with an arrow length of4
Additional Challenge
Define a new function named drawChefHatHeight
that changes the height of a chef's hat based on a height
parameter. Use the repeatWithParameter
method to call the drawChefHatHeight
function repeatedly.