Helper Functions

This page contains a list of helper generic helper functions we use either to improve readability or to reduce rewriting some functions over and over again. The goal is to make them very specific (do one thing and do it well) so they can be combined and composed easily with other functions.

add(x, y)

import { assertEquals } from "/deps.ts";
import { add } from "./add.ts";

Deno.test("add()", async (t) => {
  await t.step("should add any two numbers", () => {
    assertEquals(add(0, 0), 0);
    assertEquals(add(0, -0), 0);
    assertEquals(add(1.7, -4), -2.3);
    assertEquals(add(-1e3, -1), -1001);
  });
});
/**
 * Adds `x` and `y`.
 *
 * **TIME COMPLEXITY**: O(1). `x` and `y` are simply added once.
 *
 * **SPACE COMPLEXITY**: O(1). No extra space is required by this
 * algorithm to add the two numbers.
 *
 * @param x
 * @param y
 * @returns The value of `x` and `y` added together.
 */
function add(x: number, y: number): number {
  return x + y;
}

export { add };

isEven(n)

import { assertEquals } from "/deps.ts";
import { isEven } from "./isEven.ts";

Deno.test("isEven()", async (t) => {
  await t.step("should be true for 0, 2, -42, 1e3", () => {
    assertEquals(isEven(0), true);
    assertEquals(isEven(2), true);
    assertEquals(isEven(-42), true);
    assertEquals(isEven(1e3), true);
  });

  await t.step("should return false for 1, 3, -41, 1e3 - 1", () => {
    assertEquals(isEven(1), false);
    assertEquals(isEven(3), false);
    assertEquals(isEven(-41), false);
    assertEquals(isEven(1e3 - 1), false);
  });
});
/**
 * Adds `x` and `y`.
 *
 * **TIME COMPLEXITY**: O(1). `x` and `y` are simply added once.
 *
 * **SPACE COMPLEXITY**: O(1). No extra space is required by this
 * algorithm to add the two numbers.
 *
 * @param x
 * @param y
 * @returns The value of `x` and `y` added together.
 */
function add(x: number, y: number): number {
  return x + y;
}

export { add };