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
padStarton a number. Convert withtoFixedfirst so you have a string. - Treating Kelvin as Fahrenheit plus 273. Convert to Celsius first, or use the C-to-K function on
fToC(f). - Hardcoding the table rows instead of a sample array. Then adding 25 C means copying a whole print line.
Practice
- Change the 20 C sample to 25 C and confirm the F and K columns update.
- Add a sixth sample of -10 with
pushand confirm a new row prints. - Add
fToKthat reusesfToCandcToK, 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.