Print a conversion table from Celsius to Fahrenheit with a typed helper function.
What to build
A program that prints Celsius temperatures from a start to an end value, in steps, with their Fahrenheit equivalents.
Requirements
- Write a function
toFahrenheit(c: number): number. - For C = start, start + step, ... while C ≤ end, print
C C = F Fwith F to one decimal place, for example25 C = 77.0 F. - If start is greater than end, print
empty table.
Input
One line: start, end and step, whole numbers separated by spaces.
Output
One line per temperature, or empty table.
Constraints
-100 ≤ start, end ≤ 100; 1 ≤ step ≤ 50.
Example
Input:
0 30 10
Output:
0 C = 32.0 F
10 C = 50.0 F
20 C = 68.0 F
30 C = 86.0 F
Why it works
A typed function makes the conversion impossible to call with text by accident, and toFixed(1) keeps every row in the same format whatever the arithmetic produces.
Examples
Example 1 · Input
0 30 10
Expected output
0 C = 32.0 F 10 C = 50.0 F 20 C = 68.0 F 30 C = 86.0 F
Example 2 · Input
5 1 2
Expected output
empty table
Hints
Hint 1
Write a function toFahrenheit(c: number): number using F = C × 9 / 5 + 32.
Hint 2
toFixed(1) formats a number with one decimal place.
Solution
Show one correct solution (try it yourself first)
const read = (): string => prompt() ?? "";
const [start, end, step] = read().split(" ").map(Number);
function toFahrenheit(c: number): number {
return (c * 9) / 5 + 32;
}
if (start > end) {
console.log("empty table");
} else {
for (let c = start; c <= end; c += step) {
console.log(c + " C = " + toFahrenheit(c).toFixed(1) + " F");
}
}