Skip to main content
Version: v1.4

✏️ 3.1.1.1 Coin Counter, Sieve

Goal: Get comfortable with the basics of functional programming. Practice writing pure functions, closures, function factories, and recursive functions.

Warm Up


  • What is a pure function? What are some of the benefits of pure functions?
  • What is immutability? Why is it important in functional programming?
  • What is a closure?
  • Why would we want to build a function factory?

Code


Coin Counter

Part 1

Write a pure function coinCounter that takes a number of cents and returns an object with the coin breakdown (quarters, dimes, nickels, pennies). Here are some tests to get you started. You don't need to use these exact tests, but be sure to write tests for all parts:

describe('coinCounter', () => {
test('returns correct number of quarters', () => {
expect(coinCounter(100).quarters).toBe(4);
});

test('returns correct change for $4.99', () => {
const result = coinCounter(499);
expect(result.quarters).toBe(19);
expect(result.pennies).toBe(4);
});

test('returns an object with all four coin types', () => {
const result = coinCounter(99);
expect(result).toHaveProperty('quarters');
expect(result).toHaveProperty('dimes');
expect(result).toHaveProperty('nickels');
expect(result).toHaveProperty('pennies');
});
});

Part 2

Rewrite your coin counter function so that it uses recursion.

Part 3

Create a coin counter using a closure, with separate functions for each coin type (quarters, dimes, nickels, pennies). You can use recursion if you like.


Roman Numerals

You've already had a chance to create an application to convert numbers into Roman numerals. This time, solve the problem recursively!

Roman numerals are based on seven symbols:

Symbol  Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1,000

The most basic rule is that you add the value of all the symbols: so II is 2, LXVI is 66, etc.

The exception is that there may not be more than three of the same characters in a row. Instead, you switch to subtraction. So instead of writing IIII for 4, you write IV (for 5 minus 1); and instead of writing LXXXX for 90, you write XC.

You also have to separate ones, tens, hundreds, and thousands. In other words, 99 is XCIX, not IC. You cannot count higher than 3,999 in Roman numerals.


Prime Sifting

The goal in solving this problem is to use recursion!

Given a number, write a method that returns all of the prime numbers less than that number.

This is a tricky problem and you should use the Sieve of Eratosthenes to solve it. Here's how the Sieve of Eratosthenes works to find a number up to a given number:

  • Create a list of numbers from 2 through n: 2, 3, 4, ..., number.
  • Initially, let prime equal 2, the first prime number.
  • Starting from prime, remove all multiples of prime from the list.
  • Increment prime by 1.
  • When you reach number, all the remaining numbers in the list are primes.

You also might find this video helpful in explaining the Sieve.

Instructor/Peer Code Review


  • Code uses functional programming and does not mutate state.
  • Code is well tested.
  • Code demonstrates an understanding of closures, recursion, and other functional concepts.
  • Application works as expected.