Practice: Functions
This chapter introduces no new concepts.
💡 Why this matters: Functions, closures, recursion, and higher-order functions only really click once you’ve written them yourself. These exercises cover everything from this module. Work through them in order, run each one, and check that your output matches exactly.
Exercises
For each exercise, write the function described, call it as instructed, and confirm your logged output matches exactly.
-
Write an arrow function
isEven(n)that returnstrueifnis even andfalseotherwise. CallisEven(6)andisEven(7), logging each result. Expected output:truethenfalse. -
Write a function declaration
cube(n)that returnsn * n * n. Callcube(3)and log the result. Expected output:27. -
Rewrite
cubeas a function expression namedcubeExpr. CallcubeExpr(4)and log the result. Expected output:64. -
Rewrite
cubeonce more as a concise arrow function namedcubeArrow(no curly braces, noreturnkeyword). CallcubeArrow(5)and log the result. Expected output:125. -
Write a function
greet(name = "stranger")using a default parameter that logs"Hi, "followed byname. Callgreet()with no argument, then callgreet("Priya"). Expected output:Hi, strangerthenHi, Priya. -
Write a function
average(...nums)using a rest parameter that returns the average of any number of arguments. Callaverage(2, 4, 6)and log the result, then callaverage(10, 20)and log the result. Expected output:4then15. -
Write a function
announce(message)that only logsmessageand has noreturnstatement. Store the result of callingannounce("Testing")in a variable calledoutcome, then logoutcome. Expected output:Testingthenundefined. -
Write a function
makeStepper()that returns a function. Each time the returned function is called, it should return a number one higher than the previous call, starting at1. Createstep = makeStepper(), then callstep()three times in a row, logging each result. Expected output:1,2,3, each on its own line. -
Using
makeStepperfrom the previous exercise, create two independent steppers,step1andstep2. Callstep1(), thenstep1()again, thenstep2()once, logging each result in that order. Expected output:1,2,1, each on its own line, proving the two steppers don’t share state. -
Write a recursive function
countDownFrom(n)that logs each whole number fromndown to1, one per line, then logs"Liftoff!"when it reaches0. CallcountDownFrom(4). Expected output:4,3,2,1,Liftoff!, each on its own line. -
Write a recursive function
power(base, exponent)that returnsbaseraised toexponent, with base caseexponent === 0returning1. Callpower(2, 5)and log the result. Expected output:32. -
Write a higher-order function
applyTwice(fn, value)that callsfnonvalue, then callsfnagain on that result, and returns the final value. Write a small functionaddFive(n)that returnsn + 5. CallapplyTwice(addFive, 10)and log the result. Expected output:20. -
Write a higher-order function
makeAdder(x)that returns a function which addsxto whatever number it’s called with. Createadd10 = makeAdder(10). Calladd10(5)and log the result, then calladd10(100)and log the result. Expected output:15then110.
Next module: Arrays, Objects & Iteration, where you’ll use the functions you just learned to build, reshape, and loop over real collections of data.