Hello World
const message: string = "Hello, world!";
console.log(message);

Run with a TypeScript runtime or compile first:

npx tsx main.ts

This defines a typed string and logs it. It is the basic TypeScript shape for a tiny script.

Types
type User = {
  id: number;
  name: string;
  active: boolean;
};

const user: User = {
  id: 1,
  name: "Dan",
  active: true,
};

This type describes the expected object shape, then the object literal follows that contract.

Functions
function greet(name: string): string {
  return `Hello, ${name}!`;
}

console.log(greet("world"));

This function makes both the input and output types explicit, which is one of the main benefits of TypeScript.

TypeScript Add
function add(param1: number, param2: number): number {
  return param1 + param2;
}

This just adds the two input numbers with the language’s normal arithmetic and returns the sum.

TypeScript Add Border
function addBorder(picture: string[]): string[] {
  const width = picture[0].length;
  const bordered = picture.map((row) => `*${row}*`);
  const border = "*".repeat(width + 2);
  return [border, ...bordered, border];
}

This builds a new grid with a * border around every side. It adds a full top and bottom row, then wraps each existing row from left and right.

TypeScript Adjacent Elements Product
function adjacentElementsProduct(inputArray: number[]): number {
  let max = -Infinity;

  for (let i = 0; i < inputArray.length - 1; i++) {
    max = Math.max(max, inputArray[i] * inputArray[i + 1]);
  }

  return max;
}

This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.

TypeScript Almost Magic Square
function almostMagicSquare(a: number[]): number[] {
  const rowSum = [0, 0, 0];
  const colSum = [0, 0, 0];
  let maxSum = 0;

  const grid: number[][] = [a.slice(0, 3), a.slice(3, 6), a.slice(6, 9)];

  for (let i = 0; i < 3; i++) {
    for (let j = 0; j < 3; j++) {
      rowSum[i] += grid[i][j];
      colSum[i] += grid[j][i];
    }
  }

  for (let k = 0; k < 3; k++) {
    maxSum = Math.max(maxSum, rowSum[k]);
    maxSum = Math.max(maxSum, colSum[k]);
  }

  for (let i = 0, j = 0; i < 3 && j < 3; ) {
    const diff = Math.min(maxSum - rowSum[i], maxSum - colSum[j]);
    grid[i][j] += diff;
    rowSum[i] += diff;
    colSum[j] += diff;

    if (rowSum[i] === maxSum) {
      i++;
    }
    if (colSum[j] === maxSum) {
      j++;
    }
  }

  return grid.flat();
}

This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.

TypeScript Are Equally Strong
function areEquallyStrong(
  yourLeft: number,
  yourRight: number,
  friendsLeft: number,
  friendsRight: number
): boolean {
  return (
    Math.max(yourRight, yourLeft) === Math.max(friendsLeft, friendsRight) &&
    Math.min(yourLeft, yourRight) === Math.min(friendsRight, friendsLeft)
  );
}

This compares each person’s strongest and weakest arm. If both pairs match, the result is true.

TypeScript Array Change
function arrayChange(a: number[]): number {
  let min = 0;
  for (let k = 0; k < a.length - 1; k++) {
    if (a[k] >= a[k + 1]) {
      const dif = a[k] - a[k + 1] + 1;
      a[k + 1] += dif;
      min += dif;
    }
  }

  return min;
}

This moves left to right and bumps values only when needed so the array becomes strictly increasing.

TypeScript Array Maximal Adjacement Difference
function arrayMaximalAdjacentDifference(a: number[]): number {
  let dif = 0;
  for (let i = 1; i < a.length - 1; i++) {
    dif = Math.max(dif, Math.abs(a[i] - a[i - 1]), Math.abs(a[i] - a[i + 1]));
  }

  return dif;
}

This checks the gap between each pair of neighbors and returns the largest difference.

TypeScript Binary Gap
function binaryGap(n: number): number {
  const trimmed = n.toString(2).replace(/^0+|0+$/g, "");
  const zeroes = trimmed.split("1");

  let gap = 0;
  for (const zero of zeroes) {
    gap = Math.max(gap, zero.length);
  }

  return gap;
}

This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.

TypeScript Bracket
function bracket(s: string): number {
  const stack: string[] = [];

  for (const v of s) {
    switch (v) {
      case ")":
        if (stack.length === 0 || stack.pop() !== "(") {
          return 0;
        }
        break;
      case "]":
        if (stack.length === 0 || stack.pop() !== "[") {
          return 0;
        }
        break;
      case "}":
        if (stack.length === 0 || stack.pop() !== "{") {
          return 0;
        }
        break;
      default:
        if (v) {
          stack.push(v);
        }
        break;
    }
  }

  return stack.length === 0 ? 1 : 0;
}

This uses a simple stack approach: open brackets go in, matching closing brackets pop them out.