CodingNic

Functions

Practice: Functions

Functions 40 min read

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.

  1. Write an arrow function isEven(n) that returns true if n is even and false otherwise. Call isEven(6) and isEven(7), logging each result. Expected output: true then false.

  2. Write a function declaration cube(n) that returns n * n * n. Call cube(3) and log the result. Expected output: 27.

  3. Rewrite cube as a function expression named cubeExpr. Call cubeExpr(4) and log the result. Expected output: 64.

  4. Rewrite cube once more as a concise arrow function named cubeArrow (no curly braces, no return keyword). Call cubeArrow(5) and log the result. Expected output: 125.

  5. Write a function greet(name = "stranger") using a default parameter that logs "Hi, " followed by name. Call greet() with no argument, then call greet("Priya"). Expected output: Hi, stranger then Hi, Priya.

  6. Write a function average(...nums) using a rest parameter that returns the average of any number of arguments. Call average(2, 4, 6) and log the result, then call average(10, 20) and log the result. Expected output: 4 then 15.

  7. Write a function announce(message) that only logs message and has no return statement. Store the result of calling announce("Testing") in a variable called outcome, then log outcome. Expected output: Testing then undefined.

  8. 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 at 1. Create step = makeStepper(), then call step() three times in a row, logging each result. Expected output: 1, 2, 3, each on its own line.

  9. Using makeStepper from the previous exercise, create two independent steppers, step1 and step2. Call step1(), then step1() again, then step2() 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.

  10. Write a recursive function countDownFrom(n) that logs each whole number from n down to 1, one per line, then logs "Liftoff!" when it reaches 0. Call countDownFrom(4). Expected output: 4, 3, 2, 1, Liftoff!, each on its own line.

  11. Write a recursive function power(base, exponent) that returns base raised to exponent, with base case exponent === 0 returning 1. Call power(2, 5) and log the result. Expected output: 32.

  12. Write a higher-order function applyTwice(fn, value) that calls fn on value, then calls fn again on that result, and returns the final value. Write a small function addFive(n) that returns n + 5. Call applyTwice(addFive, 10) and log the result. Expected output: 20.

  13. Write a higher-order function makeAdder(x) that returns a function which adds x to whatever number it’s called with. Create add10 = makeAdder(10). Call add10(5) and log the result, then call add10(100) and log the result. Expected output: 15 then 110.

Next module: Arrays, Objects & Iteration, where you’ll use the functions you just learned to build, reshape, and loop over real collections of data.