This is a continuation of Converting Celsius to Fahrenheit with Python, but in TypeScript:


function c2f(c: number): number {
  return (c * 9) / 5 + 32;
}

function isMirror(a: number, b: number) {
  function massage(n: number) {
    if (n < 10) return `0${n}`;
    else if (n >= 100) return massage(n - 100);
    return `${n}`;
  }
  return reverseString(massage(a)) === massage(b);
}

function reverseString(str: string) {
  return str.split("").reverse().join("");
}

function printConversion(c: number, f: number) {
  console.log(`${c}°C ~= ${f}°F`);
}

for (let c = 4; c < 100; c += 12) {
  const f = c2f(c);
  if (isMirror(c, Math.ceil(f))) {
    printConversion(c, Math.ceil(f));
  } else if (isMirror(c, Math.floor(f))) {
    printConversion(c, Math.floor(f));
  } else {
    break;
  }
}

And when you run it:


❯ bun run conversion.ts
4°C ~= 40°F
16°C ~= 61°F
28°C ~= 82°F
40°C ~= 104°F
52°C ~= 125°F

Comments

Your email will never ever be published.

Previous:
Converting Celsius to Fahrenheit with Python July 12, 2024 Python
Next:
Converting Celsius to Fahrenheit with Go July 17, 2024 Go
Related by category:
Bun vs. Go for a basic web server benchmark October 24, 2025 Bun
Hosting your static web site with Firebase Hosting November 3, 2025 Bun
Benchmarking oxlint vs biome December 12, 2025 Bun
Testing out vite 8 on SPA: Vite 8 is 5x faster December 6, 2025 Bun
Related by keyword:
Converting Celsius to Fahrenheit round-up July 22, 2024 Python, Go, Node, JavaScript, Bun, Ruby, Rust
Comparing Deno vs Node vs Bun August 5, 2024 JavaScript, Bun
Converting Celsius to Fahrenheit with Crystal July 19, 2024 Ruby
Converting Celsius to Fahrenheit with Rust July 20, 2024 Rust