← All projects

Temperature Converter

Write temperature conversion and formatting functions.

beginnerJavaScriptFunctionsNumbers
Start building, submit solution and get feedback from the community.
2Submit Solution
5 upvotes10 upvotes

Temperature Converter

A temperature widget needs two conversion helpers and one formatting helper. Keep the math separate from the display text so each function has one clear job.

Write these functions:

  • celsiusToFahrenheit(celsius) should convert Celsius to Fahrenheit and return the number.

  • fahrenheitToCelsius(fahrenheit) should convert Fahrenheit to Celsius and return the number.

  • formatTemperature(value, unit) should return display text such as "77 F" or "20 C".

Sample checks:

js
const fahrenheit = celsiusToFahrenheit(25);console.log(formatTemperature(fahrenheit, 'F'));const celsius = fahrenheitToCelsius(68);console.log(formatTemperature(celsius, 'C'));const freezingFahrenheit = celsiusToFahrenheit(0);console.log(formatTemperature(freezingFahrenheit, 'F'));const freezingCelsius = fahrenheitToCelsius(32);console.log(formatTemperature(freezingCelsius, 'C'));

Expected output:

txt
77 F20 C32 F0 C

The conversion functions should return numbers. formatTemperature should return the final string.

Solution

Solution:

js
function celsiusToFahrenheit(celsius) {  return (celsius * 9) / 5 + 32;}function fahrenheitToCelsius(fahrenheit) {  return ((fahrenheit - 32) * 5) / 9;}function formatTemperature(value, unit) {  return `${value} ${unit}`;}const fahrenheit = celsiusToFahrenheit(25);console.log(formatTemperature(fahrenheit, 'F'));const celsius = fahrenheitToCelsius(68);console.log(formatTemperature(celsius, 'C'));const freezingFahrenheit = celsiusToFahrenheit(0);console.log(formatTemperature(freezingFahrenheit, 'F'));const freezingCelsius = fahrenheitToCelsius(32);console.log(formatTemperature(freezingCelsius, 'C'));

Join the Community

z1.jawanmard.com is among the top 10 most starred projects on GitHub and is visited by hundreds of thousands of developers every month.

Top 10 out of 28M!

373K

GitHub Stars

Star us on GitHub
Help us reach #1

+90kevery month

+3.2M

Registered Users

Register yourself
Commit to your growth

+2kevery month

52K

Discord Members

Join on Discord
Join the community

RoadmapsGuidesFAQsYouTube

z1.jawanmard.comby@nilbuild

Community created roadmaps, best practices, projects, articles, resources and journeys to help you choose your path and grow in your career.

© z1.jawanmard.com·Terms·Privacy·

ThewNewStack

The top DevOps resource for Kubernetes, cloud-native computing, and large-scale development and deployment.