Callbacks Node.js App: Individual Exercises
Complete the individual exercises to update the Callbacks app!
ASCII Arrow
- Define a new function named
drawArrowthat logs an ASCII arrow to the console:'>>------>' - Call the
repeatfunction to repeat the new function 10 times- Pass the
drawArrowfunction as the callback argument - Pass
10as the second argument
- Pass the
Repeat Two Functions
- Define a new function named
repeatTwo - The
repeatTwofunction should have three parameters:callback1,callback2, andnumberOfTimes - Copy the body of the existing
repeatfunction, and paste it into the body of therepeatTwofunction - Update the body of the
repeatTwofunction so that it calls both thecallback1andcallback2functions in thewhileloop - Finally, call the
repeatTwofunction to repeat thedrawArrowanddrawChefHatfunctions2times
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
exclaimfunction should have one parameter:message - In the body of the
exclaimfunction, declare a new variable namednewMessageusinglet - Assign the
newMessagevariable to be themessageparameter with an exclamation point (!) at the end of it- For example, if
messagewere "hello",newMessageshould be "hello!"
- For example, if
- Use
console.logto log thenewMessageto the console
Defining a New Function to Repeat Functions
- Define a new function named
repeatWithParameter - The
repeatWithParameterfunction should have three parameters:callback,callbackParameter, andnumberOfTimes - Copy the body of the existing
repeatfunction, and paste it into the body of therepeatWithParameterfunction - Update the body of the
repeatWithParameterfunction so that it passes thecallbackParameterwhen calling thecallbackfunction - Finally, call the
repeatWithParameterfunction, and make it call theexclaimfunction3times with a message of "hello"
CHALLENGE - Random Function
- Define a new function named
randomFunction - The
randomFunctionfunction should have two parameters:callback1,callback2 - In the body of the
randomFunctionfunction, declare a variable namedrandomNumberusinglet - Use
Math.random()to generate a random number between 0 and 1, and store the result in therandomNumbervariable - Use an
ifstatement to check ifrandomNumberis less than.5 - If
randomNumberis less than.5, call thecallback1function - Using an
else, ifrandomNumberis NOT less than.5, call thecallback2function
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
drawArrowLengthfunction, build a string for the arrow based on thelengthparameter- The number of hyphen characters (
-) should match thelength - For example, if
lengthwere5, the arrow would be>>-----> - If
lengthwere2, the arrow would be>>-->
- The number of hyphen characters (
- Call the
repeatWithParameterfunction, and make it call thedrawArrowLengthfunction3times 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.