TypeScript Tutorial

TypeScript Project: Temperature Converter

Convert Celsius, Fahrenheit, and Kelvin with a small set of functions and formatted output.

What you will build

Temperature conversion is a handful of formulas. Each formula lives in a function so you do not retype the arithmetic in every print. Celsius, Fahrenheit, and Kelvin all appear. Output uses toFixed so each value shows one digit after the decimal.

Sample temperatures are literals. Open Try it in TypeScript and compile at/typescript/try. Leave Python and C++ editors closed for this file.

Celsius to Fahrenheit and Kelvin

Fahrenheit is c * 9 / 5 + 32. Kelvin is c + 273.15. Write both as functions that take and return number. TypeScript numbers are already floating-point, so 9 / 5 is 1.8, not 1.

Example

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

function cToK(c: number): number {
  return c + 273.15;
}

const c: number = 20;
console.log(c + " C");
console.log(cToF(c) + " F");
console.log(cToK(c) + " K");

20 C is 68 F and 293.15 K. Change 20 to 0 to see the freezing point of water on each scale.

The other direction

Going back to Celsius is the inverse formula. Keep those in functions too, even if the file starts from Celsius. You will need them when a later input is already Fahrenheit or Kelvin.

Example

function fToC(f: number): number {
  return (f - 32) * 5 / 9;
}

function kToC(k: number): number {
  return k - 273.15;
}

console.log(fToC(68));
console.log(kToC(293.15));

Both lines should print 20, or a value extremely close to 20 given binary floating point.

Formatted columns

toFixed(1) prints one digit after the decimal. padStart sets a minimum field width so a table does not wobble when 9 becomes 100. Call toFixed first; padding a raw number would fail because padStart is a string method.

Example

function cell(n: number): string {
  return n.toFixed(1).padStart(8);
}

console.log(cell(20) + cell(68) + cell(293.2));
console.log(cell(100) + cell(212) + cell(373.2));

Compile this table at /typescript/try with Try it in TypeScript. If a column looks short, you padded before toFixed, or you skipped padStart on one of the three values.

Complete converter

Print a header, then convert a small list of Celsius values. Each row is C, F, and K. The list is anumber[] so you can add a temperature with one push.

Example

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

function cToK(c: number): number {
  return c + 273.15;
}

function cell(n: number): string {
  return n.toFixed(1).padStart(10);
}

function printRow(c: number): void {
  console.log(cell(c) + cell(cToF(c)) + cell(cToK(c)));
}

const samples: number[] = [];
samples.push(-40);
samples.push(0);
samples.push(20);
samples.push(37);
samples.push(100);

console.log("C".padStart(10) + "F".padStart(10) + "K".padStart(10));
for (const c of samples) {
  printRow(c);
}

Minus 40 is the point where C and F match. 37 is typical human body temperature. 100 is boiling water at standard pressure. Read the F and K columns and confirm the functions, not a mental chart.

Common mistakes

  • Adding 32 before multiplying. Parentheses matter: (f - 32) * 5 / 9.
  • Calling padStart on a number. Convert with toFixed first so you have a string.
  • Treating Kelvin as Fahrenheit plus 273. Convert to Celsius first, or use the C-to-K function onfToC(f).
  • Hardcoding the table rows instead of a sample array. Then adding 25 C means copying a whole print line.

Practice

  1. Change the 20 C sample to 25 C and confirm the F and K columns update.
  2. Add a sixth sample of -10 with push and confirm a new row prints.
  3. Add fToK that reuses fToC and cToK, then convert a hardcoded Fahrenheit value of 98.6 to C and K.

Next project: tic-tac-toe on a 3x3 board with a winner check.

FAQ: TypeScript Project: Temperature Converter

Common questions about this page.

What is the StudyGrid TypeScript tutorial?

The StudyGrid TypeScript tutorial follows the same chapter rhythm as C++: syntax, types, input, loops, functions, classes, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run typescript temperature project examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn typescript temperature project in this TypeScript TypeScript lesson (TypeScript Project: Temperature Converter).

Is the TypeScript editor the same as Try Python or Try C++?

No. Try TypeScript type-checks with tsc at /typescript/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. TypeScript lessons never open those editors.

Do I need to install a compiler to learn TypeScript?

No. Open a chapter, click Try it in TypeScript, and compile in the browser. You can also download a .ts file and compile locally with tsc.

Where should I start the TypeScript tutorial?

Start at TypeScript Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open TypeScript Examples, then generics, Map, and arrow functions. Use Next at the bottom of each chapter.

Is the TypeScript tutorial free?

Yes. The TypeScript workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.